更改路线时Django不会更改html

时间:2019-08-05 15:47:03

标签: django

我为每个项目功能创建了多个html,但是当更改路线时,html不变。出现的唯一html是主页index.html。

urls.py:

from website.views import IndexTemplateView, FuncionarioListView, FuncionarioUpdateView, FuncionarioCreateView, FuncionarioDeleteView
from django.conf.urls import patterns, include, url

app_name = 'website'

urlpatterns = [
# GET /
url('', IndexTemplateView.as_view(), name="index"),

# GET /funcionario/cadastrar
url('funcionario/cadastrar', FuncionarioCreateView.as_view(), name="cadastra_funcionario"),

# GET /funcionarios
url('funcionarios/', FuncionarioListView.as_view(), name="lista_funcionarios"),

# GET/POST /funcionario/{pk}
url('funcionario/<pk>', FuncionarioUpdateView.as_view(), name="atualiza_funcionario"),

# GET/POST /funcionarios/excluir/{pk}
url('funcionario/excluir/<pk>', FuncionarioDeleteView.as_view(), name="deleta_funcionario"),
]

views.py:

# -*- coding: utf-8 -*-

from django.core.urlresolvers import reverse_lazy
from django.views.generic import TemplateView, ListView, UpdateView, CreateView, DeleteView
from helloworld.models import Funcionario
from website.forms import InsereFuncionarioForm


# PÁGINA PRINCIPAL
# ----------------------------------------------

class IndexTemplateView(TemplateView):
    template_name = "website/index.html"


# LISTA DE FUNCIONÁRIOS
# ----------------------------------------------

class FuncionarioListView(ListView):
    template_name = "website/lista.html"
    model = Funcionario
    context_object_name = "funcionarios"


# CADASTRAMENTO DE FUNCIONÁRIOS
# ----------------------------------------------

class FuncionarioCreateView(CreateView):
    template_name = "website/cria.html"
    model = Funcionario
    form_class = InsereFuncionarioForm
    success_url = reverse_lazy("website:lista_funcionarios")


# ATUALIZAÇÃO DE FUNCIONÁRIOS
# ----------------------------------------------

class FuncionarioUpdateView(UpdateView):
    template_name = "website/atualiza.html"
    model = Funcionario
    fields = '__all__'
    context_object_name = 'funcionario'
    success_url = reverse_lazy("website:lista_funcionarios")


# EXCLUSÃO DE FUNCIONÁRIOS
# ----------------------------------------------

class FuncionarioDeleteView(DeleteView):
    template_name = "website/exclui.html"
    model = Funcionario
    context_object_name = 'funcionario'
    success_url = reverse_lazy("website:lista_funcionarios")

以及其中一个html的示例:

{% extends "website/_layouts/base.html" %}

{% load widget_tweaks %}

{% block title %}Cadastro de Funcionários{% endblock %}

{% block conteudo %}
<div class="container mt-5">
  <div class="row">
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
      <div class="card">
        <div class="card-body">
          <h5 class="card-title">Cadastro de Funcionário</h5>
          <p class="card-text">
            Complete o formulário abaixo para cadastrar
            um novo <code>Funcionário</code>.
          </p>
          <form method="post">
            <!-- Não se esqueça dessa tag -->
            {% csrf_token %}

            <!-- Nome -->
            <div class="input-group mb-3">
              <div class="input-group-prepend">
                <span class="input-group-text">Nome</span>
              </div>
              {% render_field form.nome class+="form-control" %}
            </div>

            <!-- Sobrenome -->
            <div class="input-group mb-3">
              <div class="input-group-prepend">
                <span class="input-group-text">Sobrenome</span>
              </div>
              {% render_field form.sobrenome class+="form-control" %}
            </div>

            <!-- CPF -->
            <div class="input-group mb-3">
              <div class="input-group-prepend">
                <span class="input-group-text">CPF</span>
              </div>
              {% render_field form.cpf class+="form-control" %}
            </div>

            <!-- Tempo de Serviço -->
            <div class="input-group mb-3">
              <div class="input-group-prepend">
                <span class="input-group-text">Tempo de Serviço</span>
              </div>
              {% render_field form.tempo_de_servico class+="form-control" %}
            </div>

            <!-- Remuneração -->
            <div class="input-group mb-3">
              <div class="input-group-prepend">
                <span class="input-group-text">Remuneração</span>
              </div>
              {% render_field form.remuneracao class+="form-control" %}
            </div>
            <hr>
            <div class="text-right">
              <a href="{% url 'website:lista_funcionarios' %}" class="btn btn-outline-primary">Voltar</a>
              <button class="btn btn-primary">Enviar</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
{% endblock %}

在更改路线(例如更改员工/注册人员)时,html不会更改,谢谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您对URL语法感到困惑。您应该始终使用path

path('', IndexTemplateView.as_view(), name="index"),
path('funcionario/cadastrar', FuncionarioCreateView.as_view(), name="cadastra_funcionario"),

path('funcionarios/', FuncionarioListView.as_view(), name="lista_funcionarios"),

path('funcionario/<pk>', FuncionarioUpdateView.as_view(), name="atualiza_funcionario"),

path('funcionario/excluir/<pk>', FuncionarioDeleteView.as_view(), name="deleta_funcionario"),

使用url,您的所有模式都将被解释为正则表达式,因此每个路径都将与第一个为空的模式匹配。

编辑如果无法使用路径,则需要确保模式是正则表达式:它们需要锚定和终止,并且必须使用正确的语法来捕获参数。 / p>

url('^$', IndexTemplateView.as_view(), name="index"),
url('^funcionario/cadastrar$', FuncionarioCreateView.as_view(), name="cadastra_funcionario"),

url('^funcionarios/$', FuncionarioListView.as_view(), name="lista_funcionarios"),

url('^funcionario/(?P<pk>.*)$', FuncionarioUpdateView.as_view(), name="atualiza_funcionario"),

url('^funcionario/excluir/(?P<pk>.*)$', FuncionarioDeleteView.as_view(), name="deleta_funcionario"),

答案 1 :(得分:0)

问题可能出在您的urls.py
这是有关正则表达式(regex)的一些基本知识。

在正则表达式中,有特殊字符:

^(脱字符号):匹配字符串的开头

'x'    # Matches 'x' in 'x'
'xyz'  # Matches 'x' in 'xyz'
'abc'  # Not match because 'x' was not at the beginning 


$(美元符号):匹配字符串的结尾。

'y'     # Matches 'y' in 'y'
'xy'    # Matches 'y' in 'xy'
'xyz'   # Not match because 'y' was not at the end


r'R'raw strings的前缀(转义序列被忽略),对正则表达式很有用,因此最好使用它。来自其文档:

  

当存在r'R'前缀时,反斜杠仍用于引用以下字符,但是所有反斜杠都保留在字符串中。例如,字符串文字r“ \ n”由两个字符组成:反斜杠和小写字母'n'。

这意味着r'\ n'等同于'\ n'。

好。足以满足正则表达式的基本要求。

因此您在此行的 urls.py
url('', IndexTemplateView.as_view(), name="index")
模式''r''的意思是:
Django会在URL中的任意位置查找空字符串,这对于每个URL都是正确的。
因此您所有的网址都将转到索引页面。

因此,您必须改用它:
url(r'^$', IndexTemplateView.as_view(), name="index"),
同时使用^$作为^$将匹配一个空行/字符串。
且仅当Django遇到一个空字符串时,它才会进入索引页面

同时,对于其他网址,您必须在每个网址的末尾使用$

urlpatterns = [
   url(r'^$', IndexTemplateView.as_view(), name="index"),

   url(r'funcionario/cadastrar/$', FuncionarioCreateView.as_view(), name="cadastra_funcionario"),

   url(r'funcionarios/$', FuncionarioListView.as_view(), name="lista_funcionarios"),

   url(r'funcionario/<pk>/$', FuncionarioUpdateView.as_view(), name="atualiza_funcionario"),

   url(r'funcionario/excluir/<pk>/$', FuncionarioDeleteView.as_view(), name="deleta_funcionario"),
]

为什么最后需要使用$
例如regex r'^hello/'
最后没有$,它将匹配任何以hello开头的网址,例如:

  • 你好/世界
  • 你好/兄弟/
  • hello / 123 / there /