我正在使用 django,构建一个 Web 应用程序。我的大部分东西都在工作。一般结构是我有一个用户绑定到一个元素列表。当我点击这些元素时,我得到一个 404 page not found 错误。
继承人一些models.py
:
class Event(models.Model):
#user that created the event
#event title
title = models.CharField(max_length=200)
#time created
created = models.DateField('Date Created',auto_now_add=True)
#description of event from user
description = models.CharField(max_length=400)
#address of event if existant
address = models.CharField(max_length=50)
class User(models.Model):
attending = models.ManyToManyField('Event', related_name='User_attending')
hosting = models.ForeignKey('Event', related_name='User_hosting')
gender = models.IntegerField(max_length=1)
password = models.CharField(max_length=100)
email = models.CharField(max_length=100)
age = models.IntegerField()
fName = models.CharField(max_length=25)
lName = models.CharField(max_length=25)
id = models.AutoField(primary_key=True)
从views.py
,当我尝试访问附加到我的“事件”对象的 html 页面时,我收到 404 错误,因为我的代码无法识别“事件”。
from list.models import *
def event(request, id):
#e = Event()
#code.interact(local = locals())
try:
e = Event.objects.get(User_hosting=id)
except Event.DoesNotExist:
raise Http404
if e.user_id != request.user.id:
raise Http404
#code.interact(local = locals())
return render_to_response('event/event.html', {'event':e})
当我使用外壳时,e = Event.objects.get(User_hosting=id)
它说“未定义事件”。