我是django的新手,对django MVT概念了解甚少,
我的问题是关于Django应用程序的,我遇到了这个愚蠢的错误,但是我不知道怎么回事,我一次又一次地检查了代码中的所有内容,并且一切正常,但是我不知道怎么不起作用。 / p>
我的错误:
Page not found (404)
Request Method: POST
Request URL: http://127.0.0.1:8000/emp
Using the URLconf defined in fusion.urls, Django tried these URL patterns, in this order:
admin/
[name='login']
home/ [name='home']
signup/ [name='signup']
employee/ [name='emp']
show/ [name='show']
edit/<int:id>
update/<int:id>
delete/<int:id>
accounts/
The current path, emp, didn't match any of these.
emp方法不会重定向到显示方法
views.py
from django.shortcuts import render,redirect
from django.contrib.auth.forms import UserCreationForm,AuthenticationForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django_adminlte.forms import EmployeeForm
from django_adminlte.models import Employee
def emp(request):
if request.method == "POST":
form = EmployeeForm (request.POST) # here "form" is one varible
if form.is_valid():
try:
form.save()
return redirect("/show")
except:
pass
else:
form = EmployeeForm()
return render(request,"employee/employee_index.html",{'form':form})
def show(request):
employees = Employee.objects.all()
return render(request,"employee/show.html",{'employees': employees})
urls.py
from django.conf.urls import url
from django.contrib import admin
from django.urls import path,include
from django_adminlte import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.login,name='login'),
path('home/',views.home,name='home'),
path('signup/',views.signup,name='signup'),
path('employee/',views.emp,name='emp'),
path('show/',views.show,name='show'),
path('edit/<int:id>',views.edit),
path('update/<int:id>',views.update),
path('delete/<int:id>',views.delete),
path('accounts/',include('django.contrib.auth.urls')),
]
HTML
{% extends 'adminlte/base.html' %}
{% block content %}
<!-- Horizontal Form -->
<form method="POST" class="post-form" action="/emp">
{% csrf_token %}
<div class="container">
<br>
<div class="form-group row">
<label class="col-sm-1 col-form-lable"></label>
<div class="col-sm-4">
<h3>Enter Details</h3>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-lable">Employee ID : </label>
<div class="col-sm-4">
{{ form.eid }}
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-lable">Employee Name : </label>
<div class="col-sm-4">
{{ form.ename }}
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-lable">Employee Email : </label>
<div class="col-sm-4">
{{ form.eemail }}
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-lable">Employee Contact :</label>
<div class="col-sm-4">
{{ form.econtact }}
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
谢谢!
答案 0 :(得分:0)
尝试将网址格式更改为以下内容:
url(r'^emp/', 'django_adminlte.views.emp', name = 'emp')
答案 1 :(得分:0)
您可以将模板中的url标记与路径引用名称一起使用。您可以从here
检查在您的示例中,您为“ employee /”路径定义了引用名称“ emp”,因此您可以同时使用<form method="POST" class="post-form" action="{% url 'emp' %}">
和<form method="POST" class="post-form" action="/employee/">
。
答案 2 :(得分:0)
def emp(request):
try:
if request.method == "POST":
form = EmployeeForm (request.POST) # here "form" is one varible
if form.is_valid():
try:
form.save()
return redirect("/show")
except:
pass
else:
form = EmployeeForm()
return render(request,"employee/employee_index.html",{'form':form})
except TemplateNotFound as e:
return render('404.html')