简体   繁体   中英

Reverse for 'note_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['notes\\/(?P<slug>[-\\w]+)/$']

NoReverseMatch at /notes/ Reverse for 'note_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['notes\\/(?P[-\\w]+)/$'] Request Method: GET Request URL: http://127.0.0.1:8000/notes/ Django Version: 2.0.3 Exception Type: NoReverseMatch Exception Value:
Reverse for 'note_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['notes\\/(?P[-\\w]+)/$'] Exception Location: C:\\Users\\auwwa\\Desktop\\notes\\lib\\site-packages\\django\\urls\\resolvers.py in _reverse_with_prefix, line 632 Python Executable: C:\\Users\\auwwa\\Desktop\\notes\\Scripts\\python.exe Python Version: 3.6.4 Python Path:
['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'] Server time: Wed, 24 Oct 2018 21:18:57 +0000

I have problem Reverse for 'note_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['notes\\/(?P[-\\w]+)/$']

this is 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)

and url 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

The note.slug in this line is not returning a value. Either there are no objects in the context value all_notes, or there is no slug, the context object name is wrong etc. If you know the value of an existing slug, just hardcore it in there to see if it works, like

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM