Django没有显示块内容

时间:2015-07-27 19:02:46

标签: python django django-templates django-views django-urls

我是Django的新手,我正在尝试设置我的项目,以便有一个主页,然后在主页上有一堆不同的链接。问题是每当我点击其他选项卡(产品,登录,联系人)等时,它只是重新加载主页块内容,尽管我告诉它加载其他东西。所以,当我点击链接时,它确实显示我在一个新的网页上(不知道我的BASE_DIR是什么'/ courses / products /',例如在网址栏中,但它始终是显示views.homepage的同一个网站。 这是我的课程/ urls.py:

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

urlpatterns = [
               url(r'$', views.homepage, name='homepage'),
               url(r'contact/$', views.contact, name='contact'),
               url(r'login/$', views.login, name='login'),
               url(r'products/$', views.products, name='products'),
               url(r'register/$', views.register, name='register'),
]

这是我的layout.html,它位于模板文件夹中:

{% load static from staticfiles %}
<!DOCTYPE html>
<html>
    <head>
        <meta charset = "utf-8">
            <title>HealthSmart</title>
            <link href="{% static 'images/photo.jpg' %}" rel='icon' type='image/x-icon' />
            <link rel="stylesheet" href="{% static 'css/normalize.css' %}" />
            <link href="http://fonts.googleapis.com/css?family=Lora|Open+Sans:400italic,700italic,400,700,300,800" rel='stylesheet' type='text/css' />
            <link href='http://fonts.googleapis.com/css?family=Varela+Round|' rel='stylesheet' type='text/css' />
            <link rel="stylesheet" href="{% static 'css/pavle.css' %}" />
            <link rel="stylesheet" href="{% static 'css/responsive.css' %}" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <header>
            <a href="{% url 'courses:homepage' %}" id="logo">
                <h1>{% block title %} {% endblock %}</h1>
            </a>
            <nav>
                <ul>
                    <li><a href="{% url 'courses:homepage' %}">Home</a></li>
                    <li><a href="{% url 'courses:products' %}">Products</a></li>
                    <li><a href="{% url 'courses:contact' %}">Contact</a></li>
                    <li><a href="{% url 'courses:login' %}">Login</a></li>
                </ul>
            </nav>
        </header>
        {% block content %} {% endblock %}
        <div id="footer">
            <footer>
                <a href="http://www.briggscorp.com/"><img src="{% static 'images/briggs.png' %}" alt="BriggsLogo" class="social-icon"></a>
                <p>&copy;2015 HealthSmart Prototypes.</p>
            </footer>
        </div>
    </body>
</html>

最后,这是我的views.py:

from django.shortcuts import get_object_or_404
from django.shortcuts import render

from .models import Owner, Device, Page

# Create your views here.

def homepage(request):
    home = Page.objects.all()
    return render(request, 'courses/homepage.html', {'homepage': homepage})

def contact(request, contact_pk):
    contact = get_object_or_404(Page, pk=contact_pk)
    return render(request, 'courses/contact.html', {'contact': contact})

def login(request, login_pk):
    login = get_object_or_404(Page, pk=login_pk)
    return render(request, 'courses/login.html', {'login': login})

def products(request, products_pk):
    product = get_object_or_404(Page, pk=products_pk)
    return render(request, 'courses/products.html', {'product': product})

def register(request, register_pk):
    register = get_object_or_404(Page, pk=register_pk)
    return render(request, 'courses/register.html', {'register': register})

另外,我将包含homepage.html和products.html以防万一: homepage.html:

{% extends "layout.html" %}

{% block title %}{{ page.title }}{% endblock %}
{% block content %}
<article>
    <div class="main_body">
        <div class="hep">
            <section class="product">
                <h3 id="purify">Purifying your life</h3>
                <p id="smartprod">Meet our smart <a href="{% url 'courses:products' %}">products</a> today.</p>
            </section>
        </div>
    </div>
    <div id="information">
        <h3>Change your Home Environment with the new Briggs HealthSmart Air Purifier and Humidifier</h3>
        <p>Run multiple devices at the same time all on your smartphone!</p>
    </div>
    <div id="getStarted">
        <p>If you have an account <a href="{% url 'courses:login' %}">Login</a>, or you can get started today and <a href="{% url 'courses:register' %}">Register</a>!</p>
    </div>
</article>
{% endblock %}

products.html放在:

{% extends "layout.html" %}

{% block title %}{{ page.title }}{% endblock %}
{% block content %}
<article>
    <div id="wrapper">
        <section>
            <ul id="gallery">
                <li>
                    <a href="{% static 'images/air purifier 03272015.png' %}">
                        <img src="{% static 'images/air purifier 03272015.png' %}" alt="" />
                        <p>The Briggs Air Purifier.</p>
                    </a>
                </li>
                <li>
                    <a href="{% static 'images/base 03272015.png' %}">
                        <img src="{% static 'images/base 03272015.png' %}" alt="" />
                        <p>The Briggs Base.</p>
                    </a>
                </li>
                <li>
                    <a href="{% static 'images/humidifier 03272015.png' %}">
                        <img src="{% static 'images/humidifier 03272015.png' %}" alt="" />
                        <p>The Briggs Air Humidifier.</p>
                    </a>
                </li>
                <li>
                    <a href="{% static 'images/water tank 03272015.png' %}">
                        <img src="{% static 'images/water tank 03272015.png' %}" alt="" />
                        <p>The Briggs Water Tank.</p>
                    </a>
                </li>
            </ul>
        </section>
    </div>
</article>
{% endblock %}

1 个答案:

答案 0 :(得分:2)

使用

       url(r'^$', views.homepage, name='homepage'),

对于您的主页,您是否还可以向我们展示您如何扩展“layout.html”?

对于您的联系问题,您可以在网址格式中添加pk

       url(r'contact/(?P<contact_pk>\d+)/$', views.contact, name='contact'),