/ notes /处的NoReverseMatch
找不到带有参数“('',)”的“ note_detail”。尝试了1种模式:['notes \ /(?P [-\ w] +)/ $']
请求方法:GET
要求网址:
Django版本:2.0.3
异常类型:NoReverseMatch
异常值:
找不到带有参数“('',)”的“ note_detail”。尝试了1种模式:['notes \ /(?P [-\ w] +)/ $']
例外位置:_reverse_with_prefix,第632行中的C:\ Users \ auwwa \ Desktop \ notes \ lib \ site-packages \ django \ urls \ resolvers.py
Python可执行文件:C:\ Users \ auwwa \ Desktop \ notes \ Scripts \ python.exe
的Python版本:3.6.4
Python路径:
['C:\ Users \ auwwa \ Desktop \ notes \ src \ notes',
'C:\ Users \ auwwa \ Desktop \ notes \ Scripts \ python36.zip',
'C:\ Users \ auwwa \ Desktop \ notes \ DLLs',
'C:\ Users \ auwwa \ Desktop \ notes \ lib',
'C:\ Users \ auwwa \ Desktop \ notes \ Scripts',
'C:\ Users \ auwwa \ AppData \ Local \ Programs \ Python \ Python36 \ Lib',
'C:\ Users \ auwwa \ AppData \ Local \ Programs \ Python \ Python36 \ DLLs',
'C:\ Users \ auwwa \ Desktop \ notes',
'C:\ Users \ auwwa \ Desktop \ notes \ lib \ site-packages']
服务器时间:2018年10月24日,星期三21:18:57 +0000
我对'note_detail'有反向问题,但找不到参数'('',)'。尝试了1种模式:['notes \ /(?P [-\ w] +)/ $']
这是viewes:
from django.shortcuts import render
from .models import Note
from django.contrib.auth.models import User
from .forms import NoteForm
# Create your views here.
def all_notes(request):
all_notes = Note.objects.all()
context = {
'all_notes':all_notes,
}
return render(request, 'all_notes.html',context)
def detail(request, slug):
note = Note.objects.get(slug=slug)
context = {
'note':note
}
return render(request, 'note_details.html', context)
def note_add(request):
if request.method == 'POST':
form = NoteForm(request.POST)
if form.is_valid():
new_form = form.save(commit=False)
new_form.user = request.user
new_form.save()
else:
form = NoteForm()
context={
'form':form
}
return render(request, 'add.html',context)
和网址nots_app:
from django.conf.urls import url
from . import views
app_name = "notes_app"
urlpatterns = [
url(r'^$', views.all_notes, name='all_notes'),
url(r'^(?P<slug>[-\w]+)/$', views.detail , name='note_detail'),
url(r'^Add$', views.note_add, name='add_note'),
]
forms.py ::
from django import forms
from .models import Note
class NoteForm(forms.ModelForm):
class Meta:
model = Note
fields = ['title', 'content', 'tags']
all-notes.html ::
<h1>Welcome in my notes</h1>
<h3>All The Available Notes</h3>
<a href="{% url 'notes:add_note' %}">Add New Notes</a>
<br>
<hr>
{% for note in all_notes %}
<a href="{% url 'notes:note_detail' note.slug %}">{{note}}</a>
<br>
{% endfor %}
notes_detail.html ::
<h1>welcome</h1>
{{note}}<br>
{{note.content}}<br>
{{note.created}}<br>
{{note.tags}} <br>
models.py :::
from django.db import models
from django.contrib.auth.models import User
import datetime
from django.utils.text import slugify
# Create your models here.
class Note(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=50)
slug = models.SlugField(null=True,blank=True)
content = models.TextField(blank=True)
created = models.DateTimeField(blank=True, default=datetime.datetime.now)
active = models.BooleanField(default=True)
tags = models.CharField(blank=True, max_length=100)
def save(self, *args, **kwargs ):
if not self.slug:
self.slug = slugify(self.title)
super(Note, self).save(*args, **kwargs )
def __str__(self):
return self.title
答案 0 :(得分:0)
此行中的note.slug没有返回值。上下文值all_notes中没有对象,或者没有子对象,则上下文对象名称不正确,等等。如果您知道现有子对象的值,只需对其进行硬核以查看其是否有效,例如