简体   繁体   English

如何在Django上动态更改块的内容?

[英]How to change the contents of a block dynamically on django?

SEE FIXED CODE BELOW 请参阅下面的固定代码

I'm working on my first web page project using django 1.8. 我正在使用django 1.8进行第一个网页项目。 My web page has a section that consists of a product menu and a column displaying a different view based on selected menu item. 我的网页有一个部分,该部分由产品菜单和一列组成,该列根据所选菜单项显示不同的视图。

At first I tried an approach where each of the menu buttons have a onclick js function assigned that changes the contents of the block. 最初,我尝试了一种方法,其中为每个菜单按钮分配了一个onclick js函数,该函数可更改块的内容。 I was thinking that I would then write the html code of each product into separate file where it would be read by the js function. 我当时在想,然后将每种产品的html代码写入单独的文件中,以供js函数读取。 I got it partially working but it feels kinda sketchy. 我得到它的部分工作,但感觉有点粗略。

My second approach was based on using django templates but I run into some errors as a beginner I couldn't quite figure out what exactly is the problem. 我的第二种方法是基于使用django模板的,但是作为一个初学者,我遇到了一些错误,我无法弄清楚到底是什么问题。 I would highly appreciate if someone could pinpoint what I'm doing wrong or tell me what's the right approach for changing the contents dynamically. 如果有人可以指出我在做什么错,或者告诉我什么是动态更改内容的正确方法,我将不胜感激。

The errors I'm getting currently: 我目前遇到的错误:

http://127.0.0.1:8000/productmonitor/
NoReverseMatch at /productmonitor/
Reverse for 'product' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'productmonitor/(?P<product_name>[a-z]+)/$']

http://127.0.0.1:8000/productmonitor/
NoReverseMatch at /productmonitor/dualpol/
Reverse for 'product' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'productmonitor/(?P<product_name>[a-z]+)/$']

urls.py urls.py

# -*- coding: utf-8 -*-
from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<product_name>[A-Za-z0-9\-\_]+)/$', views.product, name='product'),
    url(r'^information/$', views.information, name='information'),
    url(r'^casedatabase/$', views.casedatabase, name='casedatabase'),
    url(r'^contact/$', views.contact, name='contact'),
]

views.py views.py

from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from django.views import generic
# Create your views here.
from .models import Product

def index(request, product_name='default'):
    #template = 'productmonitor/index.html'
    if product_name == 'dualpol':
        template = 'productmonitor/base_productmonitor_dualpol.html'
    if product_name == 'rhi':
        template = 'productmonitor/base_productmonitor_rhi.html'

    template = 'productmonitor/base_productmonitor.html'
    test_context = {
                    'products' : Product.objects.all(),                   
                    }
    return render(request, template, test_context)

def product(request, product_name='dualpol'):
    if product_name == 'dualpol':
        template = 'productmonitor/base_productmonitor_dualpol.html'
    if product_name == 'rhi':
        template = 'productmonitor/base_productmonitor_rhi.html'

    test_context = {
                    'products' : Product.objects.all(),                   
                    }
    return render(request, template, test_context)

base_productmonitor.html base_productmonitor.html

{% extends "productmonitor/base.html" %}    
{% block content %}
    <div class="productSelectMenu">
        <ul>
            <p>Test</p>
            {% for product in products %}
                <li><a href="{% url 'productmonitor:product' 'dualpol' %}">{{ product.name }}<a/></li>  
            {% endfor %}
        </ul>
    </div>

    {% block productcontent %}
        <div id="productView" class="productView">
            <p>Default productView content</p>    
        </div>
    {% endblock %}
{% endblock %}

base_productmonitor_dualpol.html base_productmonitor_dualpol.html

{% extends "productmonitor/base_productmonitor.html" %}
{% block productcontent %}
<p>
    dualpol
</p>
{% endblock %}

EDIT: FIXED VERSION 编辑:固定版本

urls.py urls.py

# -*- coding: utf-8 -*-
from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^product/(?P<product_name>[a-z]+)/$', views.product, name='product'),
    url(r'^information/$', views.information, name='information'),
    url(r'^casedatabase/$', views.casedatabase, name='casedatabase'),
    url(r'^contact/$', views.contact, name='contact'),
]

views.py views.py

enter code here
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render, redirect
from django.views import generic
# Create your views here.
from .models import Product

def index(request, product_name=''):
    return redirect('productmonitor:product', product_name='dualpol')

def product(request, product_name):
    if product_name == 'dualpol':
        template = 'productmonitor/base_productmonitor_dualpol.html'
    if product_name == 'rhi':
        template = 'productmonitor/base_productmonitor_rhi.html'

    test_context = {
                    'products' : Product.objects.all(),                   
                    }
    return render(request, template, test_context)

def information(request):
    template = 'productmonitor/base_information.html'
    context = {}
    return render(request, template, context)

def casedatabase(request):
    template = 'productmonitor/base_case-database.html'
    context = {}
    return render(request, template, context)

def contact(request):
    template = 'productmonitor/base_contact.html'
    context = {}
    return render(request, template, context)

base_productmonitor.html base_productmonitor.html

{% extends "productmonitor/base.html" %}

{% block content %}
    <div class="productSelectMenu">
        <ul>
            <li><a href="{% url 'productmonitor:product' 'dualpol' %}">Dual pol<a/></li>
            <li><a href="{% url 'productmonitor:product' 'rhi' %}">Rhi<a/></li>
        </ul>
    </div>

    {% block productcontent %}
        <div id="productView" class="productView">
            <p>Default productView content</p>    
        </div>
    {% endblock %}
{% endblock %}

base_productmonitor_dualpol.html base_productmonitor_dualpol.html

{% extends "productmonitor/base_productmonitor.html" %}
{% block productcontent %}
<div id="productView" class="productView">
    <p>
        dualpol
    </p>
</div>
{% endblock %}

I think what the error is saying that the render in your product function is not getting the right <product_name> to use. 我认为错误是指产品功能中的渲染未获得正确的<product_name>使用。 It's showing that the arguments that it has (the <product_name> that it is trying) are none. 它表明它具有的参数(正在尝试的<product_name> )不存在。 So what you have to do in your product function is to try and make sure that you are really getting a <product_name> . 因此,在产品功能中要做的就是尝试确保您确实得到了<product_name>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM