【问题标题】:How to manually create a view?如何手动创建视图?
【发布时间】:2016-11-02 22:58:20
【问题描述】:

我有那些翻译后的网址:

url(r'^$', p_views.IndexView.as_view(), name='index'),
url(_(r'^vente-groupee/(?P<slug>[a-zA-Z0-9-_]+)/$'),
    p_views.VenteGroupeeDetailView.as_view(), name='vente_groupee'),

我在索引视图中做了这个临时重定向:

class IndexView(generic.ListView):
    template_name = 'produits/index.html'
    context_object_name = 'liste_produits'

    # !!! Temporary just to sell one item for now
    def get(self, request, *args, **kwargs):
        langue_courante = translation.get_language()
        obj = get_object_or_404(Description,
                                langue__locale__exact=langue_courante,
                                ventes_groupees__pk=1,)
        return redirect(reverse('vente_groupee',
                                kwargs={'slug': obj.slug}))

我的问题是:因为它进行了重定向,浏览器中的 url 发生了变化,并且 我不想要那个

将“vente_groupee”视图显示为网站的“根”=“/”的最佳方式应该是什么?

【问题讨论】:

  • 为什么要重定向?
  • 它写在我的问题中,在我的代码的 cmets 中:暂时只是为了暂时出售一件商品Plus 我需要根据特定项目正确获取 url,这就是为什么我不能让视图 / 直接通过 vente_groupee 视图呈现(它需要一个参数,而 / 没有)

标签: django django-views


【解决方案1】:

目前还不清楚你在追求什么。如果您只想让根 URL 始终使用带有硬编码 slug 的视图,您可以这样做:

url(r'^$', p_views.VenteGroupeeDetailView.as_view(slug="my_item_id"), name='index')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 2012-05-18
    • 2011-02-20
    • 1970-01-01
    相关资源
    最近更新 更多