关于urlpattern中的Django pk

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

我是Django的新手,我正在阅读github上的一个应用程序:

https://github.com/rogargon/myrecommendations/blob/web2-html/myrestaurants/urls.py#L18

有一个urlpattern就像

url(r'^restaurants/(?P<pk>\d+)/$',
        RestaurantDetail.as_view(),
        name='restaurant_detail')

它撤销RestaurantDetail观点,在这里:https://github.com/rogargon/myrecommendations/blob/master/myrestaurants/views.py#L36

class RestaurantDetail(DetailView):
    model = Restaurant
    template_name = 'myrestaurants/restaurant_detail.html'

    def get_context_data(self, **kwargs):
        context = super(RestaurantDetail, self).get_context_data(**kwargs)
        context['RATING_CHOICES'] = RestaurantReview.RATING_CHOICES
        return context

在这里我知道pk被设置为一个数字,表示餐馆的ID,但在html模型中,https://github.com/rogargon/myrecommendations/blob/master/myrestaurants/templates/myrestaurants/restaurant_detail.html,我没有看到任何使用pk的地方,但页面只显示了一个餐厅。你能在这个过程中如何运作pk吗?模板如何知道我想要展示哪家餐馆?为什么这种观点没有回报呢?

 {% extends "myrestaurants/base.html" %}
{% block title %}MyRestaurants - {{ restaurant.name }}{% endblock %}
{% block content %}

<span vocab="http://schema.org/" typeof="Restaurant">

<h1>
    <span property="name">{{ restaurant.name }}</span>
    {% if user == restaurant.user %}
        (<a href="{% url 'myrestaurants:restaurant_edit' restaurant.id %}">edit</a>)
    {% endif %}
</h1>

<h2>Address:</h2>
<p>
    {{ restaurant.street }}, {{ restaurant.number }} <br/>
    {{ restaurant.zipcode }} {{ restaurant.city }} <br/>
    {{ restaurant.stateOrProvince }} ({{ restaurant.country }})
</p>

<h2>
    Dishes
    {% if user.is_authenticated %}
        (<a href="{% url 'myrestaurants:dish_create' restaurant.id %}">add</a>)
    {% endif %}
</h2>
<ul>
    {% for dish in restaurant.dishes.all %}
        <li><a href="{% url 'myrestaurants:dish_detail' restaurant.id dish.id %}">
            {{ dish.name }}</a></li>
    {% empty %}<li>Sorry, no dishes for this restaurant yet.</li>
    {% endfor %}
</ul>

<h2>Reviews</h2>
{% if restaurant.restaurantreview_set.all|length > 0 %}
<span rel="aggregateRating">
    <p typeof="AggregateRating">
        Average rating <span property="ratingValue">{{ restaurant.averageRating|stringformat:".1f" }}</span>
        {% with restaurant.restaurantreview_set.all|length as reviewCount %}
        from <span property="reviewCount">{{ reviewCount }}</span> review{{ reviewCount|pluralize }}
        {% endwith %}
    </p>
</span>
<ul rel="review">
    {% for review in restaurant.restaurantreview_set.all %}
        <li typeof="Review">
            <p rel="reviewRating" typeof="Rating">
                <span property="worstRating" content="{{ RATING_CHOICES.0.0 }}"></span>
                <span property="ratingValue">{{ review.rating }}</span> star{{ review.rating|pluralize }}
                {% with RATING_CHOICES|last as best %}
                <span property="bestRating" content="{{ best.0 }}"></span>
                {% endwith %}
            </p>
            <p property="description">{% if review.comment %}{{ review.comment }}{% endif %}</p>
            <p>Created by <span property="author">{{ review.user }}</span> on
                <span property="datePublished" content="{{ review.date|date:'Y-m-d' }}">{{ review.date }}</span></p>
        </li>
    {% endfor %}
</ul>
{% endif %}

</span>

<h3>Add Review</h3>
<form action="{% url 'myrestaurants:review_create' restaurant.id %}" method="post">
    {% csrf_token %}
    Message: <textarea name="comment" id="comment" rows="4"></textarea>
    <p>Rating:</p>
    <p>{% for rate in RATING_CHOICES %}
    <input type="radio" name="rating" id="rating{{ forloop.counter }}" value="{{ rate.0 }}" />
    <label for="choice{{ forloop.counter }}">{{ rate.1 }} star{{ rate.0|pluralize }}</label>
    <br/>{% endfor %}
    </p>
    <input type="submit" value="Review" />
</form>

{% endblock %}
{% block footer %}
    Created by {{ restaurant.user }} on {{ restaurant.date }}
{% endblock %}
python django url
1个回答
0
投票

您询问的行为(“模板如何知道我想要显示哪个餐馆”)是查看行为,而不是模板行为。

模板渲染器通过视图传递restaurant值。该视图没有(显式)return,因为它是一个class-based view - 特别是DetailView的子类。 DetailView本身继承了此视图不会覆盖的标准方法,以接受PK参数并加载特定实例。

您可以阅读DetailView的源代码,以便更好地了解正在发生的事情,如果您愿意,例如https://github.com/django/django/blob/master/django/views/generic/detail.py - 但基于类的视图是一个高级主题,并不是每个项目都会使用它们。我首先关注其他领域。

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