在 Django 中创建联系我们页面时出错?
Having an error in creating contact us page in django?
models.py
class Contact(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
message = models.TextField(max_length=400)
def __str__(self):
return f"{self.first_name} {self.last_name}"
forms.py
class ContactForm(ModelForm):
class Meta:
model = Contact
fields = ["first_name", "last_name", "message"]
widgets = {
"message": Textarea(
attrs={
"placeholder": "Xabaringizni kiriting!"
}
)
}
项目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/urls.py
from django.urls import path
from . import views
from . import models
#Template tagging
app_name = 'blog'
urlpatterns = [
path('', views.PostList.as_view(template_name='index.html'), name='index'),
path('<slug:slug>/', views.post_detail, name='post_detail'),
path("contact/", views.ContactCreate.as_view(template_name='contact1.html'), name="contact"),
path("thanks/", views.thanks, name="thanks"),
]
blog/views.py
from django.shortcuts import render, get_object_or_404
from django.views import generic
from django.views.generic import ListView,DetailView,CreateView
from .models import Post, Contact
from django.urls import reverse_lazy
from django.http import HttpResponse
from .forms import CommentForm,ContactForm
class ContactCreate(CreateView):
model = Contact
form_class = ContactForm
success_url = reverse_lazy("thanks")
def thanks(request):
return HttpResponse("Rahmat Siz bilan tez orada bog`lanamiz!")
contact_page/contact1.html我所有的页面在导航栏中都是这样链接的
<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">iLawyer.uz</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 'blog:index' %}" class="nav-link">Blog</a>
</li>
<li class="nav-item">
<a href="{% url 'blog:contact' %}" class="nav-link">Biz bilan bog'laning</a>
</li>
</ul>
</div>
</div>
</nav>
<form method="POST" action="{% url 'blog:contact' %}">
{% csrf_token %}
{{ form }}
<button type="submit">SEND</button>
</form>
我在创建模型和制作视图后链接了页面,并在 urls.py 中链接了这些页面,但它没有找到联系页面,它向我显示错误:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/contact/
Raised by: blog.views.post_detail
in the terminal: Not Found: /blog/contact/
显示这个。
你能告诉我我的错误吗我没注意到。
更改 URL 模式的顺序。即post_detailURL必须放在列表的底部
urlpatterns = [
path('', views.PostList.as_view(template_name='index.html'), name='index'),
<b>path("contact/", views.ContactCreate.as_view(template_name='contact1.html'), name="contact"),
path("thanks/", views.thanks, name="thanks"),
path('<slug:slug>/', views.post_detail, name='post_detail'),</b>
]
在您的 blog/urls.py
文件中,您将模板名称引用为 'contact1.html'
。将其更改为 contact_page/contact1.html
.
models.py
class Contact(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
message = models.TextField(max_length=400)
def __str__(self):
return f"{self.first_name} {self.last_name}"
forms.py
class ContactForm(ModelForm):
class Meta:
model = Contact
fields = ["first_name", "last_name", "message"]
widgets = {
"message": Textarea(
attrs={
"placeholder": "Xabaringizni kiriting!"
}
)
}
项目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/urls.py
from django.urls import path
from . import views
from . import models
#Template tagging
app_name = 'blog'
urlpatterns = [
path('', views.PostList.as_view(template_name='index.html'), name='index'),
path('<slug:slug>/', views.post_detail, name='post_detail'),
path("contact/", views.ContactCreate.as_view(template_name='contact1.html'), name="contact"),
path("thanks/", views.thanks, name="thanks"),
]
blog/views.py
from django.shortcuts import render, get_object_or_404
from django.views import generic
from django.views.generic import ListView,DetailView,CreateView
from .models import Post, Contact
from django.urls import reverse_lazy
from django.http import HttpResponse
from .forms import CommentForm,ContactForm
class ContactCreate(CreateView):
model = Contact
form_class = ContactForm
success_url = reverse_lazy("thanks")
def thanks(request):
return HttpResponse("Rahmat Siz bilan tez orada bog`lanamiz!")
contact_page/contact1.html我所有的页面在导航栏中都是这样链接的
<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">iLawyer.uz</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 'blog:index' %}" class="nav-link">Blog</a>
</li>
<li class="nav-item">
<a href="{% url 'blog:contact' %}" class="nav-link">Biz bilan bog'laning</a>
</li>
</ul>
</div>
</div>
</nav>
<form method="POST" action="{% url 'blog:contact' %}">
{% csrf_token %}
{{ form }}
<button type="submit">SEND</button>
</form>
我在创建模型和制作视图后链接了页面,并在 urls.py 中链接了这些页面,但它没有找到联系页面,它向我显示错误:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/contact/
Raised by: blog.views.post_detail
in the terminal: Not Found: /blog/contact/
显示这个。 你能告诉我我的错误吗我没注意到。
更改 URL 模式的顺序。即post_detailURL必须放在列表的底部
urlpatterns = [
path('', views.PostList.as_view(template_name='index.html'), name='index'),
<b>path("contact/", views.ContactCreate.as_view(template_name='contact1.html'), name="contact"),
path("thanks/", views.thanks, name="thanks"),
path('<slug:slug>/', views.post_detail, name='post_detail'),</b>
]
在您的 blog/urls.py
文件中,您将模板名称引用为 'contact1.html'
。将其更改为 contact_page/contact1.html
.