我正在运行以下代码,但是当我提交create.html模板时,它将直接转到home.html,而无需在postgres db中插入任何记录。我相信,根本不会调用函数“ create”。请协助
我尝试指导该功能
* views.py *
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .models import Product
def home(request):
return render(request, 'products/home.html')
@login_required
def create(request):
if request.method =='POST':
product = Product()
product.title = request.POST['title']
product.save()
return redirect('home')
else:
return render(request, 'products/create.html')
* urls.py *
from django.urls import path,include
from . import views
urlpatterns = [
path('create',views.create,name='create'),]
* models.py *
from django.db import models
from django.contrib.auth.models import User
class Product(models.Model):
title = models.CharField(max_length=255)
def __str__(self):
return self.title
* apps.py *
from django.apps import AppConfig
class ProductsConfig(AppConfig):
name = 'products'
*主要url.py *
from django.contrib import admin
from django.urls import path,include
from products import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('products/', include('products.urls')),]
* urls.py *
from django.urls import path,include
from . import views
urlpatterns = [
path('create',views.create,name='create'),]
* create.html *
{%extends 'base.html'%}
{%block content%}
<form method="POST" action="{% url 'create' %}" enctype = "multipart/form-data">
{% csrf_token %}
Title:
<br/>
<input type="text" name = "title"/>
<br/><br/>
<input type="submit" class = "btn btn-primary" value = "Add Product"/>
</form>
{%endblock%}
我期望将记录插入数据库(postgres)中,并且我应该能够使用“ django管理”页面对其进行验证。 我可以通过“ Django管理”页面手动添加记录,但不能通过上述html表单