我试图获取要显示的帖子详细信息视图的网址,但出现错误。
我尝试搜索,但结果涉及重新格式化将运算符从=切换为==的操作,这是不正确的。
它引发了异常,我在底部添加了完整的回溯
我使用的是Django 3,该代码之前在Django 2中可用。我可以使代码在这里和那里进行一些小的更改,直到出现此错误为止。
注意:类别和子类别应用尚未添加,因此仅使用一个简单的默认值作为占位符值。我能够做到这一点没有问题。
exc
NameError("name 'newsname' is not defined")
get_response
<bound method BaseHandler._get_response of <django.core.handlers.wsgi.WSGIHandler object at 0x0446CFE8>>
request
<WSGIRequest: POST '/panel/news/add/'>
使用Django 3
在views.py的可视代码中,我看到一个pylint错误
未定义的变量“ newsname”
catname=newsname,
是
catid=newsid,
在这部分代码中
b = News(name=newstitle, short_txt=newstxtshort, body_txt=newstxt, date=2019, pic="-", writer="-", catname=newsname, catid=newsid, show=0)
从这里
def news_add(request):
if request.method == 'POST':
newstitle = request.POST.get('newstitle')
newscat = request.POST.get('newscat')
newstxtshort = request.POST.get('newstxtshort')
newstxt = request.POST.get('newstxt')
if newstitle == "" or newstxtshort == "" or newstxt == "" or newscat == "" :
error = "All Fields Required"
return render(request, 'back/error.html', {'error':error})
b = News(name=newstitle, short_txt=newstxtshort, body_txt=newstxt, date=2019, pic="-", writer="-", catname=newsname, catid=newsid, show=0)
b.save()
return redirect('news_list')
return render(request, 'back/news_add.html')
news / views.py 完整
from django.shortcuts import render, get_list_or_404, redirect
from main.models import Main
from .models import News
def news_detail(request,word):
site = Main.objects.get(pk=1)
news = News.objects.filter(name=word)
return render(request, 'front/news_detail.html', {'site':site, 'news':news})
def news_list(request):
news = News.objects.all()
return render(request, 'back/news_list.html', {'news':news})
def news_add(request):
if request.method == 'POST':
newstitle = request.POST.get('newstitle')
newscat = request.POST.get('newscat')
newstxtshort = request.POST.get('newstxtshort')
newstxt = request.POST.get('newstxt')
if newstitle == "" or newstxtshort == "" or newstxt == "" or newscat == "" :
error = "All Fields Required"
return render(request, 'back/error.html', {'error':error})
b = News(name=newstitle, short_txt=newstxtshort, body_txt=newstxt, date=2019, pic="-", writer="-", catname=newsname, catid=newsid, show=0)
b.save()
return redirect('news_list')
return render(request, 'back/news_add.html')
新闻/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^news/(?P<word>.*)/$', views.news_detail, name='news_detail'),
url(r'^panel/news/list/$', views.news_list, name='news_list'),
url(r'^panel/news/add/$', views.news_add, name='news_add'),
]
news / models.py
from __future__ import unicode_literals
from django.db import models
class News(models.Model):
name = models.CharField(max_length=50)
short_txt = models.TextField(max_length=30, default='-')
body_txt = models.TextField(max_length=30, default='-')
date = models.TextField(max_length=12, default='-')
pic = models.TextField(max_length=30, default='-')
writer = models.TextField(max_length=50, default='-')
catname = models.CharField(max_length=50, default='-')
catid = models.IntegerField(default='0')
show = models.IntegerField(default='0')
def __str__(self):
return self.name
完整追溯
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/panel/news/add/
Django Version: 3.0.6
Python Version: 3.8.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main',
'news']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\Users\taylo\django\mpv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\taylo\django\mpv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\taylo\django\mpv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\taylo\django\mpv\myproject\news\views.py", line 33, in news_add
b = News(name=newstitle, short_txt=newstxtshort, body_txt=newstxt, date=2019, pic="-", writer="-", catname=newsname, catid=newsid, show=0)
Exception Type: NameError at /panel/news/add/
Exception Value: name 'newsname' is not defined
答案 0 :(得分:2)
您没有在newsname
函数或视图中的任何地方定义news_add
变量。
b = News(name=newstitle, short_txt=newstxtshort, body_txt=newstxt, date=2019, pic="-", writer="-", catname=newsname, catid=newsid, show=0)
解决方案是将此行更改为
b = News(name=newstitle, short_txt=newstxtshort, body_txt=newstxt, date=2019, pic="-", writer="-", catname=newscat, catid=newsid, show=0)