我正在使用django,构建一个Web应用程序。我有大部分工作。一般结构是我有一个用户绑定到元素列表。当我点击这些元素时,我得到404页面未找到错误。
以下是一些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页面时,我的“event”对象被捕获,因为我的代码无法识别“事件”,我收到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})
当我使用shell时,e = Event.objects.get(User_hosting=id)
表示“事件未定义”。
对此的任何帮助都会很棒。谢谢你提前。
答案 0 :(得分:0)
您必须导入要在shell中使用的任何模块或类。试试这个(appname
是您应用的名称。
from appname.models import Event