// MARK: Special protocol to localizaze UI's placeholder
public protocol UIPlaceholderXIBLocalizable {
var localePlaceholderKey: String? { get set }
}
extension UITextField: UIPlaceholderXIBLocalizable {
@IBInspectable public var localePlaceholderKey: String? {
get { return nil }
set(key) {
placeholder = key?.localized
}
}
}
extension UISearchBar: UIPlaceholderXIBLocalizable {
@IBInspectable public var localePlaceholderKey: String? {
get { return nil }
set(key) {
placeholder = key?.localized
}
}
}
我是Django的新手,正在使用2.1版
当我浏览NoReverseMatch at/myapp/products/
Reverse for 'product_detail' not found. 'product_detail' is not a valid view function or pattern name.
Error during template rendering
In template C:\Users\User\job\mysite\myvenv\myproject\myapp\templates\base_generic.html, error at line 8
<meta name="viewport" content="width=device-width, initial-scale=1,shrink-to-fit="no">
时,
我在单击“产品”而不是获取产品名称时出现上述错误。
在http://127.0.0.1:8000/
base_generic.html
在myapp urls.py,
<meta name="viewport" content="width=device-width, initial-scale=1,shrink-to-fit="no">
<li><a href="{% url 'myapp:products'%}">Products</a></li>
At product_list.html,
{% extends "base_generic.html" %}
{% if product_list %}
{% for product in product_list %}
<a href="{{product.get_absolute_url}}">{{product.name}}</a>
{% endfor %}
{% else %}
No product
{% endif %}
{% endblock %}
在from django. urls import path
from myapp import views
app_name = 'myapp'
urlpatterns = [
path('products/', views. ProductListView.as_view(), name='products'),
path('product/<int:pk>/', views.ProductDetailView.as_view(), name='product-detail'),
]
,
models.py
在views.py,
from django.db import models
from django.urls import reverse
class Product(models.Model):
name = models.CharField(max_length=100)
description = models.TextField(max_length=200, help_text="Enter a brief description of the product.")
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('myapp/product_detail', args=[str(self.id)])
我应该怎么做才能在单击“产品”时获得产品名称,然后从那里(也单击产品名称)获得产品详细信息?
答案 0 :(得分:0)
在reverse
函数中,您需要传递app_name:name
而不是app_name/name
。
在您的情况下:
def get_absolute_url(self):
return reverse('myapp:product_detail', args=[str(self.id)])
有关完整说明,请参见docs。