我有django模板,网址和jquery的问题

时间:2017-02-10 04:41:59

标签: javascript jquery django django-templates django-urls

当我在导航栏的href标签中使用django url时最后的jquery代码不起作用但是当我删除url并放置'#'时jquery工作正常。 我认为这与django网址(可能)有关。 无论如何,我只想解决这个问题,请告诉我为什么会发生这种情况以及如何解决这个问题。下面是基本模板和url.py 顺便说一句,每个url指向一个单独的html文件。 例如。 / home /指向home.html / aboutus /指向about.html

base.html文件     

<html>
<head>
    {% load staticfiles %}
    <meta charset="UTF-8">

    <title>{% block title %}{% endblock %} | Vasu Genset Pvt. Ltd.</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    <link rel="stylesheet" href="https://bootswatch.com/paper/bootstrap.min.css">

    <link rel="stylesheet" type="text/css" href="{% static 'index/css/index.css' %}">

    <link rel="stylesheet" href="{% static 'index/css/products-style.css' %}">

    {% block style %}{% endblock %}


</head>
<body>

    <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="#" >Vasu Genset Pvt. Ltd.</a>
                <button class="navbar-toggle collapsed" data-toggle="collapse" data-target="#mydropdown">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>
            <div id="fh5co-navbar" class="collapse navbar-collapse" id="mydropdown">
                <ul class="nav navbar-nav navbar-right" id="navbar-change">
                    <li><a class="active" href="#">Home</a></li>
                    <li><a href="#">About Us</a></li>
                    <li class="dropdown">
                    <a href="/productsandservices/" class="dropdown-toggle" data-toggle="dropdown">Products & Services <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="/generatorrepair/">Generator Repair</a></li>
                            <li><a href="/generatormaintenance/">Generator Maintenance</a></li>
                            <li><a href="/alternator/">Alternator Repair & Maintenance</a></li>
                            <li><a href="/amc/">AMC Service Provider</a></li>
                            <li><a href="/spareparts/">Spare Parts</a></li>
                            <li><a href="/generatorrentalhire/">Generator Rental & Hire</a></li>
                            <li><a href="/synchronizing/">Synchronizing Panel</a></li>
                            <li><a href="/amfpanel/">AMF Panel</a></li>
                            <li><a href="acousticenclosures">Acoustic Enclosures</a></li>
                        </ul>
                    </li>
                    <li><a href="/contactus/">Contact Us</a></li>
                </ul>
            </div>
        </div>
    </nav>


    {% block body %}
    {% endblock %}

    <footer class="text-center">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-xs-offset-1 col-xs-10 col-md-offset-3 col-md-6">
                        <h4>Copyright &copy; Vasu Genset Pvt. Ltd.</h4>
                    </div>
                </div>
            </div>
    </footer>

</body>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="{% static 'index/js/bootstrap.min.js' %}"></script>

    <script>
        $('.nav  li').click(function(e) {
          console.log(e.currentTarget,"//",e.target,"//",this);
          e.stopPropagation();
          $('.nav li').removeClass('active');
          $(this).addClass('active');
        });
    </script>


</html>

urls.py

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

urlpatterns = {
    url(r'^$', views.home, name='home'),
    url(r'^home/', views.home, name='home'),
    url(r'^aboutus/', views.about, name='aboutus'),
    url(r'^productsandservices/', views.product, name='product'),
    url(r'^contactus/', views.contact, name='contactus'),
    url(r'^generatorrepair', views.generatorrepair, name='generatorrepair'),
    url(r'^generatormaintenance', views.generatormaintenance, name='generatormaintenance'),
    url(r'^alternator', views.alternator, name='alternator'),
    url(r'^amc', views.amc, name='amc'),
    url(r'^spareparts', views.spare, name='spareparts'),
    url(r'^generatorrentalhire', views.rentalhire, name='rentalhire'),
    url(r'^synchronizing', views.synchronizing, name='synchronizing'),
    url(r'^amfpanel', views.amf, name='amfpanel'),
    url(r'^acousticenclosures', views.acoustic, name='acoustic'),
}

1 个答案:

答案 0 :(得分:0)

您需要将“/”添加到其他网址(我看到您已经拥有了前四个网址中的那些)。这样Django可以访问它们:

    url(r'^generatorrepair/', views.generatorrepair, name='generatorrepair'),
    url(r'^generatormaintenance/', views.generatormaintenance, name='generatormaintenance'),
    url(r'^alternator/', views.alternator, name='alternator'),
    url(r'^amc/', views.amc, name='amc'),
    url(r'^spareparts/', views.spare, name='spareparts'),
    url(r'^generatorrentalhire/', views.rentalhire, name='rentalhire'),
    url(r'^synchronizing/', views.synchronizing, name='synchronizing'),
    url(r'^amfpanel/', views.amf, name='amfpanel'),
    url(r'^acousticenclosures/', views.acoustic, name='acoustic'),

要使脚本能够将“active”类添加到元素中,请选择“a”元素而不是“li”并使用之前的find()函数。像这样:

$('.nav a').on('click', function(e){
   console.log(e.currentTarget,"//",e.target,"//",this);
   e.stopPropagation();
   $('.nav').find('.active').removeClass('active');
   $(this).parent().addClass('active');
});