我怎样才能 link 我的 post 创建者页面到 Python 中的其他页面?
How can I to link my post creator page to the other pages in Python?
我正在弄乱它,但找不到解决方案。我收到一个错误:
raise NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch: Reverse for 'index' not found. 'index' is not a valid view function or pattern name.
我的 html 个文件 about.html
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{% static "all.css" %}">
<link rel="stylesheet" href="{% static "bootstrap.css" %}" >
<link rel="stylesheet" href="{%static "style.css" %}">
<title>Online yuridik xizmatlar</title>
</head>
<body>
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<div class="container">
<a href="{% url 'home' %}" class="navbar-brand"><i class="fas fa-balance-scale"></i></a>
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a href="{% url 'home' %}" class="nav-link">Bosh sahifa</a>
</li>
<li class="nav-item">
<a href="{% url 'about' %}" class="nav-link">Biz haqimizda</a>
</li>
<li class="nav-item">
<a href="{% url 'index' %}" class="nav-link">Blog</a>
</li>
<li class="nav-item">
<a href="{% url 'contact' %}" class="nav-link">Biz bilan bog'laning</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="about-banner">
<div class="container text-center py-5 text-white">
<h1>Bizning kompaniyamiz haqida</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis aut doloribus dolores minus</br> voluptatibus in?</p>
</div>
</div>
<div class="about-content">
<div class="container">
<div class="row py-5">
<div class="col-md-6">
<h3>What we do</h3>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Dolorem, doloremque
quidem quasi sint reiciendis rem voluptas atque ab nam non, qui excepturi unde optio cum,
omnis necessitatibus recusandae a repellat.</p>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Dolorem, doloremque
quidem quasi sint reiciendis rem voluptas atque ab nam non, qui excepturi unde optio cum,
omnis necessitatibus recusandae a repellat.</p>
</div>
<div class="col-md-6">
<img src="{% static 'assets/pexels-august-de-richelieu-4427430.jpg' %}" alt="" class="rounded-circle img-fluid about-img d-contents">
</div>
</div>
</div>
</div>
<div class="our-staff bg-dark">
<div class="container text-center text-white">
<h2>Bizning jamoa</h2>
<div class="row">
<div class="col-md-3">
<img src="{% static 'assets/person1.jpg' %}" alt="" class="rounded-circle">
<h3>Lobar Hakimova</h3>
<p>Lorem, ipsum dolor.</p>
</div>
<div class="col-md-3">
<img src="{% static 'assets/person2.jpg' %}" alt=""class="rounded-circle">
<h3>Nigora Muxtorova</h3>
<p>Lorem, ipsum dolor.</p>
</div>
<div class="col-md-3">
<img src="{% static 'assets/person3.jpg' %}" alt=""class="rounded-circle">
<h3>Suxrob Murodov</h3>
<p>Lorem, ipsum dolor.</p>
</div>
<div class="col-md-3">
<img src="{% static 'assets/person4.jpg' %}" alt=""class="rounded-circle">
<h3>Farhod Shodmonov</h3>
<p>Lorem, ipsum dolor.</p>
</div>
</div>
</div>
</div>
<!-- FOOTER -->
<footer id="main-footer" class="text-center p-4 bg-dark text-white">
<div class="container">
<div class="row">
<div class="col">
<p>Copyright ©
<span id="year"></span> Kahharoff</p>
</div>
</div>
</div>
</footer>
<script src="{% static "jquery.js" %}"></script>
<script src="{% static "popper.js" %}"></script>
<script src="{% static "bootstrap.js" %}"></script>
<script>
// Get the current year for the copyright
$('#year').text(new Date().getFullYear());
</script>
</body>
</html>
models.py(我创建了一个post创建者模型,管理员只能创建一个post)
from django.db import models
from django.contrib.auth.models import User
STATUS = (
(0,"Draft"),
(1,"Publish")
)
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
updated_on = models.DateTimeField(auto_now= True)
content = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
app/views.py
from django.shortcuts import render
from django.views import generic
from django.views.generic import ListView,DetailView
from .models import Post
class PostList(generic.ListView):
queryset = Post.objects.filter(status=1).order_by('-created_on')
template_name = 'index.html'
class PostDetail(generic.DetailView):
model = Post
template_name = 'post_detail.html'
app/urls.py
from django.conf.urls import url
from . import views
from . import models
#Template tagging
app_name = 'blog'
urlpatterns = [
url(r'^list/$', views.PostList.as_view(template_name='index.html'), name='index'),
url(r'<slug:slug>/', views.PostDetail.as_view(template_name='post_detail.html'), name='post_detail'),
]
项目views.py
from django.shortcuts import render
from django.views.generic import TemplateView
class HomePage(TemplateView):
template_name = 'home.html'
class AboutPage(TemplateView):
template_name = 'about.html'
class ContactPage(TemplateView):
template_name = 'contact1.html'
项目urls.py
from django.contrib import admin
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$',views.HomePage.as_view(), name='home'),
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.urls',namespace='blog')),
#url(r'^blog/',include('django.contrib.auth.models.urls')),
url(r'^about/', views.AboutPage.as_view(),name='about'),
url(r'^contact/', views.ContactPage.as_view(),name='contact'),
]
您在此处包含带有命名空间 (blog
) 的 blog.urls
:
url(r'^blog/', include('blog.urls',namespace='blog')),
因此 url
标签也应该包含命名空间:
{% url 'blog:index' %}
我正在弄乱它,但找不到解决方案。我收到一个错误:
raise NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch: Reverse for 'index' not found. 'index' is not a valid view function or pattern name.
我的 html 个文件 about.html
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{% static "all.css" %}">
<link rel="stylesheet" href="{% static "bootstrap.css" %}" >
<link rel="stylesheet" href="{%static "style.css" %}">
<title>Online yuridik xizmatlar</title>
</head>
<body>
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<div class="container">
<a href="{% url 'home' %}" class="navbar-brand"><i class="fas fa-balance-scale"></i></a>
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a href="{% url 'home' %}" class="nav-link">Bosh sahifa</a>
</li>
<li class="nav-item">
<a href="{% url 'about' %}" class="nav-link">Biz haqimizda</a>
</li>
<li class="nav-item">
<a href="{% url 'index' %}" class="nav-link">Blog</a>
</li>
<li class="nav-item">
<a href="{% url 'contact' %}" class="nav-link">Biz bilan bog'laning</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="about-banner">
<div class="container text-center py-5 text-white">
<h1>Bizning kompaniyamiz haqida</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis aut doloribus dolores minus</br> voluptatibus in?</p>
</div>
</div>
<div class="about-content">
<div class="container">
<div class="row py-5">
<div class="col-md-6">
<h3>What we do</h3>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Dolorem, doloremque
quidem quasi sint reiciendis rem voluptas atque ab nam non, qui excepturi unde optio cum,
omnis necessitatibus recusandae a repellat.</p>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Dolorem, doloremque
quidem quasi sint reiciendis rem voluptas atque ab nam non, qui excepturi unde optio cum,
omnis necessitatibus recusandae a repellat.</p>
</div>
<div class="col-md-6">
<img src="{% static 'assets/pexels-august-de-richelieu-4427430.jpg' %}" alt="" class="rounded-circle img-fluid about-img d-contents">
</div>
</div>
</div>
</div>
<div class="our-staff bg-dark">
<div class="container text-center text-white">
<h2>Bizning jamoa</h2>
<div class="row">
<div class="col-md-3">
<img src="{% static 'assets/person1.jpg' %}" alt="" class="rounded-circle">
<h3>Lobar Hakimova</h3>
<p>Lorem, ipsum dolor.</p>
</div>
<div class="col-md-3">
<img src="{% static 'assets/person2.jpg' %}" alt=""class="rounded-circle">
<h3>Nigora Muxtorova</h3>
<p>Lorem, ipsum dolor.</p>
</div>
<div class="col-md-3">
<img src="{% static 'assets/person3.jpg' %}" alt=""class="rounded-circle">
<h3>Suxrob Murodov</h3>
<p>Lorem, ipsum dolor.</p>
</div>
<div class="col-md-3">
<img src="{% static 'assets/person4.jpg' %}" alt=""class="rounded-circle">
<h3>Farhod Shodmonov</h3>
<p>Lorem, ipsum dolor.</p>
</div>
</div>
</div>
</div>
<!-- FOOTER -->
<footer id="main-footer" class="text-center p-4 bg-dark text-white">
<div class="container">
<div class="row">
<div class="col">
<p>Copyright ©
<span id="year"></span> Kahharoff</p>
</div>
</div>
</div>
</footer>
<script src="{% static "jquery.js" %}"></script>
<script src="{% static "popper.js" %}"></script>
<script src="{% static "bootstrap.js" %}"></script>
<script>
// Get the current year for the copyright
$('#year').text(new Date().getFullYear());
</script>
</body>
</html>
models.py(我创建了一个post创建者模型,管理员只能创建一个post)
from django.db import models
from django.contrib.auth.models import User
STATUS = (
(0,"Draft"),
(1,"Publish")
)
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
updated_on = models.DateTimeField(auto_now= True)
content = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
app/views.py
from django.shortcuts import render
from django.views import generic
from django.views.generic import ListView,DetailView
from .models import Post
class PostList(generic.ListView):
queryset = Post.objects.filter(status=1).order_by('-created_on')
template_name = 'index.html'
class PostDetail(generic.DetailView):
model = Post
template_name = 'post_detail.html'
app/urls.py
from django.conf.urls import url
from . import views
from . import models
#Template tagging
app_name = 'blog'
urlpatterns = [
url(r'^list/$', views.PostList.as_view(template_name='index.html'), name='index'),
url(r'<slug:slug>/', views.PostDetail.as_view(template_name='post_detail.html'), name='post_detail'),
]
项目views.py
from django.shortcuts import render
from django.views.generic import TemplateView
class HomePage(TemplateView):
template_name = 'home.html'
class AboutPage(TemplateView):
template_name = 'about.html'
class ContactPage(TemplateView):
template_name = 'contact1.html'
项目urls.py
from django.contrib import admin
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$',views.HomePage.as_view(), name='home'),
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.urls',namespace='blog')),
#url(r'^blog/',include('django.contrib.auth.models.urls')),
url(r'^about/', views.AboutPage.as_view(),name='about'),
url(r'^contact/', views.ContactPage.as_view(),name='contact'),
]
您在此处包含带有命名空间 (blog
) 的 blog.urls
:
url(r'^blog/', include('blog.urls',namespace='blog')),
因此 url
标签也应该包含命名空间:
{% url 'blog:index' %}