Django自带的django-cities和django-countries url配置示例
时间: 2024-03-10 21:49:09 浏览: 196
以下是使用Django自带的django-cities和django-countries应用程序的URL配置示例:
1. 在您的Django项目的urls.py文件中导入django-countries和django-cities的视图:
```python
from django.urls import path
from cities.views import CityDetailView
from django_countries.views import country_detail
```
2. 配置django-countries的URL:
```python
urlpatterns = [
# ...
path('countries/<str:code>/', country_detail, name='country_detail'),
# ...
]
```
3. 配置django-cities的URL:
```python
urlpatterns = [
# ...
path('cities/<int:pk>/', CityDetailView.as_view(), name='city_detail'),
# ...
]
```
4. 在您的模板中使用django-countries和django-cities的URL:
```html
{% load countries %}
{% load cities %}
{% for country in countries %}
<a href="{% url 'country_detail' code=country.code %}">{{ country.name }}</a>
{% endfor %}
{% for city in cities %}
<a href="{% url 'city_detail' pk=city.pk %}">{{ city.name }}</a>
{% endfor %}
```
以上示例是使用Django自带的django-countries和django-cities应用程序的URL配置示例,您可以根据自己的需要进行修改和扩展。
阅读全文