首页 > 解决方案 > 如何映射/调度 Django url?

问题描述

我正在尝试制作一些地图......以在市场上使用。

所以我尝试使用 Django,Python。但是有一些与 url 映射相关的问题。

这就是问题所在。

jeobjeom_code = c[5] = pk = SAF11050

我想创建一些链接,例如“RAW_SoMae_MaeJang_Info_detail/SAF11050”,但没有成功。

如果 jeobjeom_code 只是 int,它可以工作!但如果 jeobjeom_code 是字符串,则根本不起作用。

我猜 c[5] 使用是错误的......但我不知道如何解决它......

NoReverseMatch at /Maps_DaeLi_PanMae Reverse for 'RAW_SoMae_MaeJang_Info_detail' 关键字参数 '{'pk': 'c[5]'}' 未找到。

-models.py

class RawSomaeMaejangInfo(models.Model):
    jeobjeom_code = models.CharField(db_column='JeobJeom_Code', primary_key=True, max_length=50)  # Field name made lowercase.
    latitude = models.CharField(db_column='Latitude', max_length=100, blank=True, null=True)  # Field name made lowercase.
    longitude = models.CharField(db_column='Longitude', max_length=100, blank=True, null=True)  # Field name made lowercase.

-urls.py

urlpatterns =[
    path('RAW_SoMae_MaeJang_Info_detail/<int=pk>', views.RAW_SoMae_MaeJang_Info_detail, name='RAW_SoMae_MaeJang_Info_detail'),

-Maps_DaeLi_PanMae.html

                      var tmpl = '<div style="font-family: dotum, arial, sans-serif;font-size: 18px; font-weight: bold;margin-bottom: 5px;">#{title}</div>' +
                          '<table style="border-spacing: 2px; border:s 0px"><tbody><tr>' +
                          '<tr><td style="color:#767676;padding-right:12px">check</td>' +
                          '<td><a href="{% url 'RAW_SoMae_MaeJang_Info_detail' pk='c[5]' %}" >link</a></td>' +

-views.py

def Maps_DaeLi_PanMae(request):
    Maps_DaeLiJeom_list = RawSomaeMaejangInfo.objects.all()
    context = {"Maps_DaeLiJeom_list":Maps_DaeLiJeom_list}
    return render(request, 'app/Maps_DaeLi_PanMae.html', {"Maps_DaeLiJeom_list":Maps_DaeLiJeom_list})

标签: djangourlmappingdispatcher

解决方案


虽然我不明白c[5]的目的是什么.. 我在您的代码中发现了两个可能导致问题的错误..

你的网址格式应该是这样的..

path('RAW_SoMae_MaeJang_Info_detail/<int:pk>/', views.RAW_SoMae_MaeJang_Info_detail, name='RAW_SoMae_MaeJang_Info_detail'),

在模板中,您已经遍历Maps_DaeLiJeom_list ..

{% for Maps_DaeLiJeom in Maps_DaeLiJeom_list %}
    <a href="{% url 'RAW_SoMae_MaeJang_Info_detail' pk=Maps_DaeLiJeom.pk %}" >link</a>
{% endfor %}

推荐阅读