Python 错误:与#x27相反;文章细节';没有找到';文章细节';不是有效的视图函数或模式名称
我试图在我的一个模型函数中使用reverse,但是当我把url的名称放在后面时,它显示了标题的错误,我不知道为什么会发生这种情况,下面是url和模型的代码 models.pyPython 错误:与#x27相反;文章细节';没有找到';文章细节';不是有效的视图函数或模式名称,python,django,url,post,frame,Python,Django,Url,Post,Frame,我试图在我的一个模型函数中使用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):
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/url.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')
]
如果有人能告诉我为什么会出现这个错误,我会非常感激,我肯定它在models.py的最后一行,但我不明白为什么会弹出,因为名称与我要调用的url匹配。你忘了提到应用程序的名称。 试试这个
也许这会解决这个问题,看起来很相似:
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')
]
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))]