简体   繁体   English

如何使用带有变量作为 url 路径的 Django url 模板标签

[英]How to use Django url template tag with variable as url path

I encountered a No Reverse Match Error in trying to render a template.我在尝试渲染模板时遇到了无反向匹配错误。 Can you explain what this No Reverse Match Error mean for easier understanding and how to solve the error?你能解释一下这个 No Reverse Match Error 是什么意思以便更容易理解以及如何解决这个错误吗?

models.py模型.py

from django.contrib.sites.models import Site
from django.contrib.gis.db import models
from django.utils.crypto import get_random_string
from django.contrib.gis.geos import GEOSGeometry,Point
from django.contrib.auth.models import AbstractUser
from django.shortcuts import render
# Create your models here.
class User(AbstractUser):
    pass

geos_pnt=Point(4314498.56, 1003834.600,srid=3857)
#pnt=GEOSGeometry('POINT(4314498.56, 1003834.600)').wkt
class Apartment(models.Model):
    SUBCITY_CHOICES = (
    ('ADK', 'Addis Ketema'), ('AKLTY', 'Akaki-Kality'), ('ARD', 'Arada'), ('BL', 'Bole'), ('GL', 'Gulele'),
    ('KLF', 'Kolfe-Keranio'), ('LDTA', 'Ledeta'), ('NFS', 'Nefas Silk'), ('YK', 'Yeka'))
    apt_id = models.CharField(str(SUBCITY_CHOICES)+"-"+get_random_string(length=4), max_length=8,primary_key=True)
    location = models.PointField(default=geos_pnt,extent=(4282586.10,996190.90,4346411.02,1011478.31),
                                    blank=True, null=True, srid=3857, help_text="Point(longitude latitude)")
    no_bedrooms= models.IntegerField(null=True)
    apt_area = models.IntegerField(default=0, null=True)
    apt_cost = models.IntegerField(default=0, null=True)
    apt_subcity = models.CharField(default='Nefas Silk',max_length=100, choices=SUBCITY_CHOICES,null=True)
    register_date = models.DateTimeField('Register Date',auto_now_add=True,null=True)
    slug = models.SlugField(unique=True)
    objects = models.Manager()
    sites =models.ManyToManyField(Site)

    #change points from apt_rent_db to kml
    def pointkml(self):
        points = Apartment.objects.kml()
        return render("placemarks.kml", {'places': points})
    def get_absolute_url(self):
        return reverse('apartment_create', kwargs={'pk': self.pk, 'apt_id': self.apt_id.pk})

    def save(self, *args, **kwargs):
        #self.Latitude = self..y
        #self.Longitude = self.location.x
        self.slug = slugify(self.apt_id)
        super(Apartment, self).save(*args, **kwargs)

    class Meta:
       # order of drop-down list items
       verbose_name = ("Apartment")
       verbose_name_plural = ("Apartments")
       ordering = ('apt_cost',)
       app_label = 'rent_app'

    def __unicode__(self):
        return self.apt_id

urls.py:网址.py:

from django.urls import path
from . import views
app_name = 'rent_app'


    urlpatterns = [

            path('', views.IndexView.as_view(), name='index'),
            path('apartment_create/<slug:apt_id>)', views.ApartmentCreate.as_view(), name='apartment_create'),
            path('apartments/<int:pk>/', views.ApartmentUpdate.as_view(), name='apartment_update'),
            path('apartments/<int:pk>/delete/', views.ApartmentDelete.as_view(), name='apartment_delete'),
             ]

views.py:视图.py:

from django.urls import reverse_lazy
from django.views import generic
from django.views.generic.edit import CreateView, DeleteView, UpdateView
from .models import Apartment
from .forms import ApartmentCreateForm


class IndexView(generic.ListView):

    template_name = 'djnago_leaflet.html'
    context_object_name = 'latest_apartments_list'

    def get_queryset(self):

        """Return the last five published apartments."""

        return Apartment.objects.order_by('-register_date')[:5]

class ApartmentCreate(CreateView):
    template_name = 'rent_app/apartment-create-success.html'
    form_class = ApartmentCreateForm
    fields = ['apt_id','location','apt_area','apt_cost']
    success_url= reverse_lazy('apartment-create')

class ApartmentUpdate(UpdateView):
    model = Apartment
    fields = ['apt_id','location','apt_area', 'apt_cost']
    template_name='index_leaflet.html'
    template_name_suffix='apartments'

class ApartmentDelete(DeleteView):
    model = Apartment
    template_name = 'index_leaflet.html'
    template_name_suffix='apartments'
    success_url = reverse_lazy('apartment-list')

the html: html:

<html>
  <head>
   {% leaflet_js plugins="forms" %}
   {% leaflet_css plugins="forms" %}
  </head>


  <body>

    {% leaflet_map "map" callback="window.map_init_basic" %}
    <h2>Edit Apartment ID {{ Apartment.apt_id }}</h2>
    <h2>Edit Apartment Location {{ Apartment.location }}</h2>
    <form action="{% url 'rent_app:apartment_create' apt_id %}" method="post">
        {% csrf_token %}
        {{ form }}
        <input type="submit"/>
    </form>
  </body>
</html>

I think there are a number of issues in your code.我认为您的代码中有很多问题。

First regarding the error you are seeing.首先关于您看到的错误。 In your urls you have defined a pattern with the name 'apartment_create' which expects a slug as a parameter.在您的 url 中,您定义了一个名为“apartment_create”的模式,它需要一个 slug 作为参数。 However, apt_id in your template is an empty field.但是,模板中的apt_id是一个空字段。 So django cannot find a pattern with the name 'apartment_create' and a valid slug.因此 django 找不到名称为“apartment_create”和有效 slug 的模式。 To solve this change the url pattern to要解决此问题,请将 url 模式更改为

path('apartment_create/', views.ApartmentCreate.as_view(), name='apartment_create')

And in your template either take out the apt_id from your action in the form (or take out the action all together.在您的模板中,要么从表单中的操作中取出apt_id (或一起取出action

However, in your ApartmenCreate view you are missing the model parameter.但是,在您的ApartmenCreate视图中,您缺少model参数。 In addition the fields and the form parameter are redundant.此外, fieldsform参数是多余的。 And are you sure the template_name parameter in that view is correct?您确定该视图中的template_name参数正确吗?

In your model the field apt_id looks strange.在您的模型中,字段apt_id看起来很奇怪。 You are creating a field with some very obscure verbose_name .您正在创建一个带有一些非常晦涩的verbose_name的字段。 If you want to have a choice field you have to set the choices parameter of the field, eg:如果你想有一个选择字段,你必须设置该字段的选择参数,例如:

apt_id = models.CharField(choices=SUBCITY_CHOICES, max_length=8, primary_key=True)

Your get_absolute_url is also not correct: First of all there is no valid url matching that pattern and secondly a field ( apt_id ) does not have a pk.您的get_absolute_url也不正确:首先,没有匹配该模式的有效 url,其次,字段 ( apt_id ) 没有 pk。

In your template you have statements like {{ Apartment.apt_id }} .在您的模板中,您有{{ Apartment.apt_id }}类的语句。 However, Apartment is a class.但是, Apartment是一个类。 So in your views add context_object_name='apartment' so you can access the values in your template like so {{ apartment.apt_id }} .因此,在您的视图中add context_object_name='apartment'以便您可以像这样访问模板中的值{{ apartment.apt_id }}

There are probably some other issues which I have overlooked.可能还有一些我忽略的其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM