django (404)-页面未找到页面未找到

5uzkadbs  于 2023-10-21  发布在  Go
关注(0)|答案(2)|浏览(128)

我被困了一段时间我不知道出了什么问题。我想显示关于客户的所有详细信息(DetailView),我得到错误404。以下是返回的URL:http://127.0.0.1:8000/poussaim/nouria/'当我删除url末尾的撇号时,页面会正确显示:http://127.0.0.1:8000/poussaim/nouria/
先谢谢你的帮助。
这是我的代码
urls.py

from django.urls import path

from poussaim_app.views import *

app_name = "nrn"

urlpatterns = [
    path("contact/", signup, name="contact"),
    # Customer
    path("", HomeViewCustomer.as_view(), name="home"),
    path("<str:slug>/", DetailViewCustomer.as_view(), name="view-customer"),
    path("<str:slug>/deletecustomer/", DeleteViewCustomer.as_view(), name="delete-customer"),

views.py

from django.urls import reverse_lazy, reverse
from django.views.generic import CreateView, UpdateView, DeleteView, ListView, DetailView
from django.contrib.auth.mixins import LoginRequiredMixin
from poussaim_app.models import Customer

class HomeViewCustomer(ListView):
    """
    Page d'accueil, page principale avec la liste des clients enrégistrés

    """
    model = Customer  
    template_name = "index.html"
    context_object_name = "customes

class DetailViewCustomer(DetailView):
    """
    Vue info/détail d'un client

    """
    model = Customer
    template_name = "client/detail_client.html"
    context_object_name = "custome"

class DeleteViewCustomer(DeleteView):
    """
    Suppression d'un client

    """
    model = Customer
    template_name = "client/delete.html"
    success_url = reverse_lazy("nrn:home")

models.py

class Customer(models.Model):
    """
    Table client: la liste des entreprises partenaires
    name: nom de l'entreprise

    """
    customer_name = models.CharField(max_length=255, unique=True, verbose_name="Nom du Client")

    gerant = models.CharField(max_length=255, blank=True,null=True, verbose_name="Nom du dirigeant ou son représentant")

    save_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)

    slug = models.SlugField(blank=True)

    class Meta:
        verbose_name = "Customer"
        ordering = ["-date_creation", "customer_name"]

    def __str__(self):
        return self.customer_name
    
    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.customer_name)
        super().save(*args, **kwargs)
    
    def get_absolute_url(self):
        return reverse("custome", kwargs={"slug": self.slug})

client/detail_client.html

{% block content %}
    <a href="{% url 'nrn:delete-customer' slug=custome.slug %}"> Supprimer </a>
    <hr>
    <div id="client">
        <h3> Nom du client: {{ custome.customer_name }} </h3>
        <h3>Contact: </h3>
        <div>
            <ul>
                <li>{{ custome.gerant }} </li>
                <li>{{ custome.save_by }} </li>
            </ul>
        </div>
    </div>
{% endblock %}
hmae6n7t

hmae6n7t1#

我不知道你是否已经在你的主www.example.com文件中配置了你的应用程序URL以-“/poussaim/”开头url.py。但是正如你所说的,当你删除撇号时,链接就可以工作了,它应该是这样的。当有撇号时,你得到错误404的原因是因为你没有配置它。尝试使用:-

path("<str:slug>/'", DetailViewCustomer.as_view(), name="view-customer"),

在这里,您已经指定它带有撇号。

ldfqzlk8

ldfqzlk82#

感谢你的帮助.我终于发现了我的错误:这是在我的一个模板html文件(在href),其中有这个额外的撇号。谢谢

相关问题