【问题标题】:Reverse for 'note_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['notes\\/(?P<slug>[-\\w]+)/$']未找到带有参数“(”,)”的“note_detail”的反向。尝试了 1 种模式:['notes\\/(?P<slug>[-\\w]+)/$']
【发布时间】:2018-10-24 21:34:04
【问题描述】:

NoReverseMatch 在 /notes/ 未找到带有参数“(”,)”的“note_detail”的反向操作。尝试了 1 种模式:['notes\/(?P[-\w]+)/$'] 请求方法:GET 请求网址:http://127.0.0.1:8000/notes/ Django 版本:2.0.3 异常类型:NoReverseMatch 异常值:
未找到带有参数“(”,)”的“note_detail”的反向操作。尝试了 1 种模式:['notes\/(?P[-\w]+)/$'] 异常位置:C:\Users\auwwa\Desktop\notes\lib\site-packages\django\urls\resolvers.py 在 _reverse_with_prefix,第 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\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

我有问题 Reverse for 'note_detail' with arguments '('',)' not found。尝试了 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)

和网址 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

【问题讨论】:

  • 这个错误是什么意思 Reverse for 'note_detail' with arguments '('',)' not found.尝试了 1 种模式:['notes\\/(?P[-\\w]+)/$']
  • 问题出在模板中,你构造了一个URL,但是没有设置合适的参数。
  • 这些模板::

    欢迎来到我的笔记

    所有可用的笔记

    添加新笔记

    {% for note in all_notes %} {{note}}
    {% endfor %}

标签: python django reverse


【解决方案1】:

这一行中的 note.slug 没有返回值。上下文值 all_notes 中没有对象,或者没有 slug,上下文对象名称错误等。如果您知道现有 slug 的值,只需在其中进行硬核以查看它是否有效,例如

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-20
    • 2021-03-26
    • 2020-05-18
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多