错误:反向' home'参数'()'和关键字参数' {}'未找到。尝试过0种模式:[]

时间:2017-01-02 19:42:06

标签: django python-2.7

项目名称是project1,我有一个应用程序 - home。我在index.html中制作了一个导航栏。但是,index.html中的第一个<li>标记给了我错误,而且我不确定如何修复它。

home&#39; urls.py

from django.conf.urls import url
from . import views

app_name = 'home'

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^portfolio/$', views.portfolio, name='portfolio'),
    url(r'^blog/$', views.blog, name='blog'),
    url(r'^contact/$', views.contact, name='contact'),
]

home的#views.py

def index(request):
    return render(request, 'home/index.html')

home的index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <title>My Website</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  {% load staticfiles %}
  <link rel="stylesheet" href="{% static 'home/css/bootstrap.css' %}">
  <link rel="stylesheet" href="{% static 'home/css/basic.css' %}">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>

<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#">Pranav Gupta</a>
    </div>
    <ul class="nav navbar-nav">
      {% url 'home' as home %}
      <li {% if request.path == home %} class="active" {% endif %} ><a href="{% url 'home' %}">Home</a></li>
      <li><a href="#">Portfolio</a></li>
      <li><a href="#">Blog</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </div>
</nav>

2 个答案:

答案 0 :(得分:3)

您没有名为home的网址,但您将其设置为{%url'home'作为主页%}

您的根网址被命名为index,将主页更改为索引。

答案 1 :(得分:1)

home是您的应用程序命名空间。您的网址名为index

您应该使用{% url 'home:index' %}