我使用的是Django 1.11和python 2.7。 我现在已经被困了几个小时,因为我无法找到它的起源。
错误是:
Internal Server Error: /
NoReverseMatch at /
Reverse for 'show_news' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['general/show_news/(?P<news_id>\\d+)/$']
我的观点:
def list_news(request):
news_items = NewsItem.objects.all().order_by('-submission_date')
return render(request, 'general/news_list.html', {'news_items': news_items})
def show_newsitem(request, news_id):
news_item = get_object_or_404(NewsItem, pk=news_id)
return render(request, 'general/news_item.html', {'news_item': news_item})
我的news_list.html模板:
{% block content %}
{% if news_items %}
{% for i in news_items %}
<div onclick="location.href='{% url 'show_news' news_id=i.pk %}'">
<div class="panel panel-default">
<div class="panel-heading">{{ i.title }}</div>
<div class="panel-body">{{ i.body|truncatewords:20 }}
<img height="100px" align="left" src="{{i.picture.url}}" alt="here">
</div>
<div class="panel-footer">{{ i.submission_date }}</div>
</div>
</div>
{% endfor %}
{% endif %}
{% endblock %}
我的网址 - 很温暖,我很匆忙而且它让我不知所措 - :
from general import views as general_views
urlpatterns = [
...
url(r'^show_news/(?P<news_id>\d+)/$', general_views.show_newsitem, name='show_news'),
]
在本地,一切正常,但在服务器上,没有任何作用! 我真的卡住了。请帮忙。 提前谢谢。