找不到参数为“(“”,)”的“note\u detail”的反转。尝试了1个模式:['notes\\/(?P<slug>[\\w]+)/$']

2024-04-19 17:24:28 发布

您现在位置:Python中文网/ 问答频道 /正文

NoReverseMatch at/注释/ 找不到参数为“(“”,)”的“note\u detail”的反转。尝试了1个模式:['notes\/(?P[-\w]+)/$'] 请求方法:GET 请求URL:http://127.0.0.1:8000/notes/ Django版本:2.0.3 异常类型:NoReverseMatch 异常值:
找不到参数为“(“”,)”的“note\u detail”的反转。尝试了1个模式:['notes\/(?P[-\w]+)/$'] 异常位置:C:\Users\auwwa\Desktop\notes\lib\site packages\django\url\分解器.py在第632行,带前缀的反方向 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\DLL', '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\u detail”有问题。尝试了1个模式:['notes\/(?P[-\w]+)/$']

这是视图:

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)

和url nots\ U应用程序:

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'),
]

你知道吗表单.py::

from django import forms
from .models import Note

class NoteForm(forms.ModelForm):    
    class Meta:
        model = Note
        fields = ['title', 'content', 'tags']

全部-便笺.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 %}

注意事项_详细信息.html::

<h1>welcome</h1>

{{note}}<br>
{{note.content}}<br>
{{note.created}}<br>
{{note.tags}} <br>

你知道吗型号.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

Tags: djangofromimportformurlmodelsrequestall
1条回答
网友
1楼 · 发布于 2024-04-19 17:24:28

你知道吗

注:slug在这行中没有返回值。要么上下文值中没有对象,要么没有slug,上下文对象名称错误等等。如果你知道一个现有slug的值,就把它硬放在那里看看它是否工作,比如 你知道吗

相关问题 更多 >