Python 使用django选择一个随机页面
我试图用Django选择一个随机页面,但我不知道如何调用该函数 我试着用一个URL来调用它,但没有成功 视图。py:Python 使用django选择一个随机页面,python,html,django,Python,Html,Django,我试图用Django选择一个随机页面,但我不知道如何调用该函数 我试着用一个URL来调用它,但没有成功 视图。py: def random_page(request): entries = util.list_entries() # list of wikis selected_page = random.choice(entries) return render(request, "encyclopedia/layout.html", {
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
})
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")
]
{% 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>
url.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
})
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")
]
{% 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>
来自。导入视图
app_name=“百科全书”
URL模式=[
路径(“,views.index,name=“index”),
路径(“wiki/”,views.wiki\u page,name=“wiki\u page”),
路径(“创建”,views.add\u条目,name=“add\u条目”),
路径(“搜索”,views.search,name=“搜索”),
路径(“wiki/edit/”,views.edit\u页面,name=“edit\u页面”)
]
layout.html:
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
})
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")
]
{% 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>
{%load static%}
{%block title%}{%endblock%}
维基
{%csrf_令牌%}
{%block body%}
{%endblock%}
为什么不重定向到给定页面。因此,可以将浏览器重定向到具有给定页面标题的页面。这样做的好处是使它更简单,只需编写一次重定向逻辑
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)
如果您随后访问
/random
,它将重定向到一个随机页面。为什么不重定向到给定页面。因此,可以将浏览器重定向到具有给定页面标题的页面。这样做的好处是使它更简单,只需编写一次重定向逻辑
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)
如果您随后访问
/random
,它将重定向到一个随机页面。我是这样写视图的
def random_page(request):
entries = util.list_entries()
selected_page = random.choice(entries)
return HttpResponseRedirect(reverse('wiki', args=[selected_page]))
我是这样写的
def random_page(request):
entries = util.list_entries()
selected_page = random.choice(entries)
return HttpResponseRedirect(reverse('wiki', args=[selected_page]))
@马塞洛弗雷塔斯:你定义了一条额外的路径,并链接到该路径?@marcelofreitas:你定义了一条额外的路径,并链接到该路径?