【问题标题】:select an random page with django用 django 选择一个随机页面
【发布时间】:2020-07-08 15:02:33
【问题描述】:

我正在尝试使用 Django 选择一个随机页面,但我不知道如何调用该函数。

我尝试使用 URL 调用它,但它不起作用。

views.py:

def random_page(request):
    entries = util.list_entries() # list of wikis
    selected_page = random.choice(entries)
    return render(request, "encyclopedia/layout.html", {
        "random_page": selected_page
    })

urls.py:

from . import views
app_name = "encyclopedia"

urlpatterns = [
    path("", views.index, name="index"),
    path("wiki/<str:page_title>", views.wiki_page, name="wiki_page"),
    path("create", views.add_entry, name="add_entry"),
    path("search", views.search, name="search"),
    path("wiki/edit/<str:page_title>", views.edit_page, name="edit_page")
]

layout.html:

{% load static %}

<!DOCTYPE html>

<html lang="en">
    <head>
        <title>{% block title %}{% endblock %}</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
        <link href="{% static 'encyclopedia/styles.css' %}" rel="stylesheet">
    </head> 
    <body>
        <h2>Wiki</h2>
        <form action="{% url 'encyclopedia:search' %}" method="POST">
            {% csrf_token %}
            <input class="search" type="text" name="q" placeholder="Search Encyclopedia">
        </form>
            <a href="{% url 'encyclopedia:index' %}">Home</a>
            <a href="{% url 'encyclopedia:add_entry' %}">Create New Page</a> 
            <a href=wiki/{{ random_page }}>Random Page</a>
        {% block body %}
        {% endblock %}
    </body>
</html>

【问题讨论】:

    标签: python html django


    【解决方案1】:

    为什么不重定向到给定的页面。因此,您将浏览器重定向到具有给定页面标题的页面。这样做的好处是你让它更简单,只需要编写一次重定向逻辑。

    from django.shortcuts import redirect
    
    def random_page(request):
        entries = util.list_entries() # list of wikis
        selected_page = random.choice(entries)
        return redirect('encyclopedia:wiki_page', page_title=selected_page)

    然后您在 urls.py 中指定路径:

    urlpatterns = [
        path("", views.index, name="index"),
        path("random/", views.random_page, name="index"),
        path("wiki/<str:page_title>", views.wiki_page, name="wiki_page"),
        path("create", views.add_entry, name="add_entry"),
        path("search", views.search, name="search"),
        path("wiki/edit/<str:page_title>", views.edit_page, name="edit_page")
    ]

    如果您随后访问/random,它将重定向到一个随机页面。

    【讨论】:

    • @marcelofreitas:你定义了一个额外的路径,并链接到那个?
    【解决方案2】:

    我是这样写视图的

    def random_page(request):
        entries = util.list_entries()
        selected_page = random.choice(entries)
        return HttpResponseRedirect(reverse('wiki', args=[selected_page]))
    

    【讨论】:

      猜你喜欢
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      相关资源
      最近更新 更多