我正在使用django 1.9.5和Python 2.7.11构建一个django应用程序。我的项目(我将其命名为djan_web)目录如下所示:
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
,但是当我点击按钮upload-file/
时,除了网址之外没有其他任何操作,href
会附加到其中。
我也尝试过使用TemplateView,因此我将按钮定义中的href="{% url 'upload' %}"
部分更改为urls.py
,并将url(r'^upload/$', TemplateView.as_view(template_name='upload.html'), name='upload')
中的第三个网址模式更改为
upload
并删除views.py
中的<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="aboutus.html">About Us</a>
</li>
<li>
<a href="services.html">Services</a>
</li>
<li>
<a href="factory.html">Factory</a>
</li>
<li>
<a href="projects.html">Projects</a>
</li>
<li>
<a href="client.html">Clients</a>
</li>
<li>
<a href="video.html">Video</a>
</li>
<li>
<a href="contactus.html">Contact Us</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<div class="row">
<img src="img/logo%20corner.png" class="center-block img-responsive imglogo">
</div>
<!-- Full Page Image Background Carousel Header -->
<header id="myCarousel" class="carousel slide">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for Slides -->
<div class="carousel-inner">
<div class="item active">
<!-- Set the first background image using inline CSS below. -->
<div class="fill" style="background-image:url('img/slide1.jpg');"></div>
<div class="carousel-caption">
</div>
</div>
<div class="item">
<!-- Set the second background image using inline CSS below. -->
<div class="fill" style="background-image:url('img/slide2.jpg');"></div>
<div class="carousel-caption">
</div>
</div>
<div class="item">
<!-- Set the third background image using inline CSS below. -->
<div class="fill" style="background-image:url('img/slide3.jpg');"></div>
<div class="carousel-caption">
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="icon-prev"></span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="icon-next"></span>
</a>
</header>
功能,但我无法正常工作。非常感谢任何帮助!
答案 0 :(得分:0)
您需要在urls.py
中修复您的网址。尝试:
urlpatterns = [
url(r'^$', views.homepage, name="homepage"),
url(r'^admin/', admin.site.urls),
url(r'^upload-file/$', views.upload, name='upload')
]
注意正则表达式末尾的$
。您需要在网址模式中添加结束锚点,尤其是第一个网址模式。没有结尾$
,它就会匹配任何内容,因此任何进入的请求都会被定向到您的主页。