主要问题是在views.py queryset上创建!我已尝试使用一个包含一些行的现有数据库。。。没有什么问题!但是,如果我从管理面板中删除所有行,那么它会显示以下错误django.db.utils.OperationalError:没有这样的表:home_general
如果表是空的,这些查询就会出错
general = General.objects.first()
social = Social_URI.objects.all()
seo = SEO.objects.first()
project_data = Project.objects.all()
如果我在内部绑定这些,请尝试,否则不会发生类似django.db.utils.OperationalError:没有这样的表:的错误,但由于空行,我在模板上再次面临错误!
from django.shortcuts import render, redirect
from django.views.decorators.http import require_http_methods
from .models import Event, Comment
from home.models import General, Social_URI, SEO
from projects.models import Project
try:
general = General.objects.first()
except:
general = None
try:
social = Social_URI.objects.all()
except:
social = None
try:
seo = SEO.objects.first()
except:
seo = None
try:
event_data = Event.objects.all()
except:
event_data = None
try:
project_data = Project.objects.all()
except:
project_data = None
def event(request):
try:
data = {
"logo": general.logo,
"favicon": general.favicon,
"title": general.title,
"top_phone": general.top_phone,
"top_email": general.top_email,
"about_image": general.about_image,
"about_title": general.about_title,
"about_text": general.about_text,
"footer_logo": general.footer_logo,
"footer_text": general.footer_text,
"footer_copyright": general.footer_copyright,
"footer_address": general.footer_address,
"social_url": social,
"ga_id": seo.ga_id,
"seo_author": seo.author,
"seo_description": seo.description,
"seo_keywords": seo.keywords,
"events_data": event_data,
"projects_data": project_data
}
except:
data = None
return render(request, "events/event.html", data)
def event_view(request, id):
event_view_data = Event.objects.get(pk=id)
comments = Comment.objects.filter(event=id)
data = {
"logo": general.logo,
"favicon": general.favicon,
"title": general.title,
"top_phone": general.top_phone,
"top_email": general.top_email,
"about_image": general.about_image,
"about_title": general.about_title,
"about_text": general.about_text,
"footer_logo": general.footer_logo,
"footer_text": general.footer_text,
"footer_copyright": general.footer_copyright,
"footer_address": general.footer_address,
"social_url": social,
"ga_id": seo.ga_id,
"seo_author": seo.author,
"seo_description": seo.description,
"seo_keywords": seo.keywords,
"event_id": event_view_data.id,
"event_title": event_view_data.title,
"event_thumb": event_view_data.thumb.url,
"event_desc": event_view_data.description,
"event_amount": event_view_data.amount,
"event_location": event_view_data.location,
"event_calender": event_view_data.calender,
"comments": comments,
"projects_data": project_data
}
return render(request, "events/event_view.html", data)
@require_http_methods(["POST"])
def comment(request):
event_id = request.POST.get("event_id")
username = request.POST.get("username")
email = request.POST.get("email")
message = request.POST.get("message")
event = Event.objects.get(pk=event_id)
comment = Comment(event=event, username=username, email=email, message=message)
comment.save()
return redirect("event_view", id=event_id)
Exception Value:
Reverse for 'event_view' with arguments '('',)' not found. 1 pattern(s) tried: ['event_view/(?P<id>[0-9]+)\Z']
您好!
通常,django.db.utils.OperationalError: no such table
发生在您在models.py
中创建模型时,但不运行迁移到新创建的数据库,更多信息请参阅:Django:OperationalError No Soke Table
其中对于Reverse for 'event_view' with arguments '('',)' not found. 1 pattern(s) tried: ['event_view/(?P<id>[0-9]+)\Z']
:它表示您试图在没有id
的情况下打开urlevent_view/
。它必须是类似event_view/1/
的东西,更多内容在这里:反转';view_item';带有参数';(';';,(';以及关键字arguments';{}';未找到