Error: Reverse for 'article-detail' not found. 'article-detail' is not a valid view function or pattern name
Error: Reverse for 'article-detail' not found. 'article-detail' is not a valid view function or pattern name
我试图在我的一个模型函数中使用 reverse,但是当我将 url 的名称倒过来时,它显示标题错误,我不确定为什么会这样,这里是 url 和模型的代码。
models.py
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length= 255)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('article-detail', args =(str(self.id)))
app/urls.py
from django.urls import path
from app1 import views
from .views import PostView, ArticleDetailView, AddPostView
app_name = 'app1'
urlpatterns = [
path('register/', views.register, name = 'register'),
path('user_login/', views.user_login, name = 'user_login'),
path('post/', PostView.as_view(), name = 'Post'),
path('article/<int:pk>', ArticleDetailView.as_view(), name = 'article-detail'),
path('add_post/',AddPostView.as_view(), name='addpost')
]
urls.py
from django.contrib import admin
from django.urls import path, include
from app1 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name = "index"),
path('app1/', include('app1.urls')),
path('logout/', views.user_logout, name='logout')
]
如果有人能告诉我为什么显示此错误,我将不胜感激,我确定它在 models.py 的最后一行,但我不明白为什么会弹出,因为该名称与我要调用的 url 匹配。
您忘记提及 app_name。
试试这个
class Post(models.Model):
title = models.CharField(max_length= 255)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
# return reverse('article-detail', args =(str(self.id)))
return reverse('app1:article-detail', args = [str(self.id))]
我试图在我的一个模型函数中使用 reverse,但是当我将 url 的名称倒过来时,它显示标题错误,我不确定为什么会这样,这里是 url 和模型的代码。
models.py
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length= 255)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('article-detail', args =(str(self.id)))
app/urls.py
from django.urls import path
from app1 import views
from .views import PostView, ArticleDetailView, AddPostView
app_name = 'app1'
urlpatterns = [
path('register/', views.register, name = 'register'),
path('user_login/', views.user_login, name = 'user_login'),
path('post/', PostView.as_view(), name = 'Post'),
path('article/<int:pk>', ArticleDetailView.as_view(), name = 'article-detail'),
path('add_post/',AddPostView.as_view(), name='addpost')
]
urls.py
from django.contrib import admin
from django.urls import path, include
from app1 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name = "index"),
path('app1/', include('app1.urls')),
path('logout/', views.user_logout, name='logout')
]
如果有人能告诉我为什么显示此错误,我将不胜感激,我确定它在 models.py 的最后一行,但我不明白为什么会弹出,因为该名称与我要调用的 url 匹配。
您忘记提及 app_name。 试试这个
class Post(models.Model):
title = models.CharField(max_length= 255)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
# return reverse('article-detail', args =(str(self.id)))
return reverse('app1:article-detail', args = [str(self.id))]