我正在用django 1.9.5和Python 2.7.11构建一个django应用程序。我的项目(我叫djan_web)目录如下所示:Django:如何通过点击一个按钮切换到另一个视图?
djan_web\
manage.py
djan_frontend\
views.py
templates\
djan_frontend\
upload.html
djan_homepage\
index.html
djan_web/
urls.py
我能够加载index.html
,这是我的主页在这里。在index.html
,我有一个按钮,点击时,我想加载upload.html
。这里是我的相关文件:
index.html:
<!DOCTYPE html>
<html>
<head>
<title >Django Wed Project</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
</head>
<body>
<div class="header">
<p> some text </p>
<div class="container" style="width:95%">
<center>
<div class="col-md-1 center-block text-center" style="font-size: xx-large">
<a href="/upload-file" class="dark" style="cursor: pointer;">
<span class="glyphicon glyphicon-upload"></span>Let's get started!</a>
</div>
</center>
</div>
</div>
</body>
</html>
urls.py:
from django.conf.urls import include, url
from django.contrib import admin
from djan_frontend import views
from django.views.generic import TemplateView
urlpatterns = [
url(r'^', views.homepage, name="homepage"),
url(r'^admin/', admin.site.urls),
url(r'^upload-file/$', views.upload, name='upload')
]
views.py:
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.core.urlresolvers import reverse
from .upload_file import UploadFileForm
from .models import Document
from .tasks import process_csv
def homepage(request):
return render(request, 'djan_frontend/djan_homepage/index.html')
def upload(request):
return render(request, 'djan_frontend/upload.html')
我能够加载index.html
,但是当我点击按钮Let's get started
诺斯除了URL,其中upload-file/
被附加到URL之外。
我也尝试过使用TemplateView,所以我在按钮定义改变href
部分href="{% url 'upload' %}"
和改变了urls.py
第三URL模式
url(r'^upload/$', TemplateView.as_view(template_name='upload.html'), name='upload')
和views.py
删除upload
功能,但我不能让这行得通。任何帮助是极大的赞赏!
你有没有试过把'/'放在''''上传文件的结尾? –
也许应该是'TemplateView.as_view(template_name ='djan_frontend/upload.html')'或者存在误印? –
仅供参考,您的HTML正文中有2个未关闭的div标签。 –