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

问题描述 投票:0回答:1

我在尝试渲染模板时遇到无反向匹配错误。您能解释一下“无反向匹配错误”是什么意思,以便于理解和解决错误吗?

models.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:

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:

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

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>
python django url
1个回答
0
投票

我认为您的代码中存在许多问题。

首先关于您看到的错误。在您的网址中,您定义了一个名称为'apartment_create'的模式,该模式需要一个slug作为参数。但是,模板中的apt_id为空字段。因此,django无法找到名称为“ apartment_create”和有效子弹的模式。要解决此问题,请将网址格式更改为

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

并且在您的模板中,从您的操作中以表格的形式取出apt_id(或一起取出action

但是,在ApartmenCreate视图中,缺少model参数。另外fieldsform参数是多余的。并且您确定该视图中的template_name参数正确吗?

在您的模型中,字段apt_id看起来很奇怪。您正在创建一个非常模糊的verbose_name字段。如果要有一个选择字段,则必须设置该字段的choices参数,例如:]

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

您的get_absolute_url也不正确:首先,没有与该模式匹配的有效url,其次,字段(apt_id)没有pk。

在模板中,您有类似{{ Apartment.apt_id }}的语句。但是,Apartment是一类。因此,在您的视图add context_object_name='apartment'中,您可以像{{ apartment.apt_id }}一样访问模板中的值。

可能还有其他一些我忽略的问题。

© www.soinside.com 2019 - 2024. All rights reserved.