我仍在使用民意调查应用程序,但进行了一些更改,以便在每个页面上回答多个问题并提供常见的“提交”功能。按钮。在这些变化之后,出现了一个奇怪的错误。
view.index是一个简单的问题列表,其中包含指向链接重定向视图的链接,该视图仅捕获用户名,然后加载包含问题和用户数据的页面。 (可能不是最好的方式,但它确实有效)。
class IndexView(generic.ListView):
template_name = 'polls/_index.html'
context_object_name = 'question_set'
def get_queryset(self):
return Question.objects.filter(man_index__gte='1').order_by('man_index')
@login_required(redirect_field_name=None)
def qlink(request, man_index):
user_id = request.user.username
question = get_object_or_404(Question, man_index = int(man_index))
page_ref = question.man_page
return HttpResponseRedirect(reverse('polls:page', args=(user_id, page_ref)))
首先出现的错误是重定向投掷未被识别的用户'在尝试加载一个民意调查的网址之后//但是它没有点击“qlink”#39;查看 - 而错误来自views.answer,处理答案的视图。
来自我的urls.py:
app_name = 'polls'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^qlink/(?P<man_index>[0-9]+)/$', views.qlink, name='qlink'),
url(r'^(?P<man_index>[0-9]+)/results/$', views.results, name='results'),
url(r'^analytics/(?P<question_id>[0-9]+)/(?P<n>[0-9]+)/$', views.analytics, name='analytics'),
url(r'^hashtags/(?P<question_id>[0-9]+)/$', views.page_hashtag_detail, name='hashtags'),
url(r'download/$', views.write_out, name = 'download'),
url(r'^(?P<user_id>\w+)/pg(?P<page_num>[0-9]+)/$', views.pageView, name='page'),
url(r'^(?P<user_id>\w+)/$', views.answer, name='answer'),
#url(r'^(?P<user_id>\w+)/(?P<man_index>[0-9]+)/short_answer/', views.short_answer, name='short_answer'),
#url(r'^(?P<user_id>\w+)/(?P<man_index>[0-9]+)/vote/', views.vote, name='vote'),
#url(r'^(?P<user_id>\w+)/(?P<man_index>[0-9]+)/long_answer/', views.long_answer, name='long_answer'),
url(r'^charts/contribution$', views.contribution_chart, name='contribution_chart'),
url(r'^(?P<user_id>\w+)/(?P<man_index>[0-9]+)/answer_delete/', views.answer_delete, name='answer_delete'),
url(r'^(?P<user_id>\w+)/(?P<man_index>[0-9]+)/brainstorm/', views.brainstorm, name='page_brainstorm'),
]
这是在index.html中单击以获取错误的链接:
{% for question in question_set %}
<li><a href="{% url 'polls:qlink' question.man_index %}"/> {{question.man_index}} {{ question.question_text }}</a></li>
{% endfor %}
这是错误:
DoesNotExist at /polls/1/
User matching query does not exist.
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/1/
Django Version: 1.11.4
Exception Type: DoesNotExist
Exception Value:
User matching query does not exist.
Exception Location: C:\Users\cruicksa\AppData\Local\Continuum\Anaconda3\lib\site-packages\django\db\models\query.py in get, line 380
Python Executable: C:\Users\cruicksa\AppData\Local\Continuum\Anaconda3\python.exe
Python Version: 3.6.0
Python Path:
['H:\\Workspace\\Django Code\\mysite6',
'C:\\Users\\cruicksa\\AppData\\Local\\Continuum\\Anaconda3\\python36.zip',
'C:\\Users\\cruicksa\\AppData\\Local\\Continuum\\Anaconda3\\DLLs',
'C:\\Users\\cruicksa\\AppData\\Local\\Continuum\\Anaconda3\\lib',
'C:\\Users\\cruicksa\\AppData\\Local\\Continuum\\Anaconda3',
'C:\\Users\\cruicksa\\AppData\\Local\\Continuum\\Anaconda3\\lib\\site-packages',
'C:\\Users\\cruicksa\\AppData\\Local\\Continuum\\Anaconda3\\lib\\site-packages\\Sphinx-1.5.1-py3.6.egg',
'C:\\Users\\cruicksa\\AppData\\Local\\Continuum\\Anaconda3\\lib\\site-packages\\pdfrw-0.3-py3.6.egg',
'C:\\Users\\cruicksa\\AppData\\Local\\Continuum\\Anaconda3\\lib\\site-packages\\win32',
'C:\\Users\\cruicksa\\AppData\\Local\\Continuum\\Anaconda3\\lib\\site-packages\\win32\\lib',
'C:\\Users\\cruicksa\\AppData\\Local\\Continuum\\Anaconda3\\lib\\site-packages\\Pythonwin',
'C:\\Users\\cruicksa\\AppData\\Local\\Continuum\\Anaconda3\\lib\\site-packages\\setuptools-27.2.0-py3.6.egg']
Server time: Thu, 17 Aug 2017 04:37:15 +0000