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!"
                }
            )
        }

Projet 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 toutes mes pages sont liées comme ceci dans la barre de navigation

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

J'ai lié les pages après avoir créé les modèles et effectué les vues et lié ces pages dans urls.py mais il ne trouve pas la page de contact, il me montre une erreur:

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/ 

Montrant cela. Pourriez-vous me montrer mon erreur que je n'ai pas pu remarquer

-1
Agabyok 3 sept. 2020 à 04:41

2 réponses

Meilleure réponse

Modifiez l'ordre des modèles d'URL. Autrement dit, l'URL doit être placée en bas de la liste

urlpatterns = [
    path('', views.PostList.as_view(template_name='index.html'), name='index'),
    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'),

]
1
Arakkal Abu 3 sept. 2020 à 02:25

Dans votre fichier blog/urls.py, vous faites référence à votre nom de modèle comme 'contact1.html'. Changez-le en contact_page/contact1.html.

0
Mubashar javed 3 sept. 2020 à 02:26