我尝试添加一个以用户作为外键的项目。 当我提交前端表单时,什么都没有发生。 我回到提交表单页面,但是项目未在数据库中注册。 即使调试打开,我也没有错误消息。
models.py
from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from template.models import Template
from colorfield.fields import ColorField
from django.utils import timezone
def Project_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return "%s/%s" % (instance.user.username, filename)
def Project_uploads_directory_path(instance, imgs, filename):
# file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return "%s/%s" % (instance.user.username, imgs, filename)
class Tag(models.Model):
name = models.CharField(max_length=255)
description = models.CharField(max_length=255, null=True, default='')
def __str__(self):
return self.name
class Project(models.Model):
user = models.OneToOneField('auth.User', on_delete=models.CASCADE)
status = models.CharField(max_length=200, default='Building')
name = models.CharField(max_length=200)
url = models.URLField()
description = models.TextField()
#tags = models.OneToOneField(Tag, on_delete=models.CASCADE)
image = models.FileField(upload_to=Project_directory_path)
create_date = models.DateTimeField(default=timezone.now)
updated = models.DateTimeField(auto_now=True)
template = models.ForeignKey(Template, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
slogan = models.CharField(max_length=200)
adresse = models.CharField(max_length=200)
phone = models.CharField(max_length=200)
mail = models.EmailField()
focus1_titre = models.CharField(max_length=200)
focus1_text = models.TextField()
focus1_img = models.FileField(upload_to=Project_uploads_directory_path)
focus1_btn = models.CharField(max_length=200)
focus1_btn_link = models.CharField(max_length=200)
...
def __str__(self):
return self.title
forms.py
class AddProjectForm(forms.ModelForm):
class Meta:
model = Project
widgets = {"user": forms.HiddenInput()}
fields = ('name', 'url', 'description', 'image', 'create_date', 'template', 'title', 'slogan', 'focus1_titre',
'focus1_text', 'focus1_img', 'focus1_btn',
'focus1_btn_link', 'focus2_titre', 'focus2_text', 'focus2_img', 'focus2_btn', 'focus3_titre',
'focus3_text', 'focus3_img', 'focus3_btn', 'focus3_btn_link',
'about_titre', 'about_text', 'about_img', 'prod1_titre', 'prod1_text', 'prod1_img', 'prod2_titre',
'prod2_text', 'prod2_img', 'prod3_titre', 'prod3_text', 'prod3_img', 'adresse', 'phone', 'mail', 'twitter', 'facebook',
'primaire', 'secondaire', 'title_font', 'text_font', )
views.py
class AddProjectView(LoginRequiredMixin, FormView):
model = Project
template_name = 'projects/add_project.html'
form_class = AddProjectForm
def get_initial(self):
user = self.request.user
initial = super().get_initial()
initial['id'] = user.id
return initial
def form_valid(self, form):
form = self.form_class(request.POST, request.FILES)
if form.is_valid():
project = form.save(commit=False) # this is missing in your views
project.user = self.request.user.id
project.name = form.cleaned_data['name']
project.url = form.cleaned_data['url']
project.description = form.cleaned_data['description']
project.image = form.cleaned_data['image']
project.template = form.cleaned_data['template']
project.title = form.cleaned_data['title']
project.slogan = form.cleaned_data['slogan']
project.adresse = form.cleaned_data['adresse']
project.phone = form.cleaned_data['phone']
project.mail = form.cleaned_data['mail']
project.about_titre = form.cleaned_data['about_titre']
project.about_text = form.cleaned_data['about_text']
project.about_img = form.cleaned_data['about_img']
project.focus1_titre = form.cleaned_data['focus1_titre']
project.focus1_text = form.cleaned_data['focus1_text']
project.focus1_img = form.cleaned_data['focus1_img']
project.focus1_btn = form.cleaned_data['focus1_btn']
project.focus1_btn_link = form.cleaned_data['focus1_btn_link']
project.focus2_titre = form.cleaned_data['focus2_titre']
project.focus2_text = form.cleaned_data['focus2_text']
project.focus2_img = form.cleaned_data['focus2_img']
project.focus2_btn = form.cleaned_data['focus2_btn']
project.focus2_btn_link = form.cleaned_data['focus2_btn_link']
project.focus3_titre = form.cleaned_data['focus3_titre']
project.focus3_text = form.cleaned_data['focus3_text']
project.focus3_img = form.cleaned_data['focus3_img']
project.focus3_btn = form.cleaned_data['focus3_btn']
project.focus3_btn_link = form.cleaned_data['focus3_btn_link']
project.prod1_titre = form.cleaned_data['prod1_titre']
project.prod1_text = form.cleaned_data['prod1_text']
project.prod1_img = form.cleaned_data['prod1_img']
project.prod2_titre = form.cleaned_data['prod2_titre']
project.prod2_text = form.cleaned_data['prod2_text']
project.prod2_img = form.cleaned_data['prod2_img']
project.prod3_titre = form.cleaned_data['prod3_titre']
project.prod3_text = form.cleaned_data['prod3_text']
project.prod3_img = form.cleaned_data['prod3_img']
project.twitter = form.cleaned_data['twitter']
project.facebook = form.cleaned_data['facebook']
project.primaire = form.cleaned_data['primaire']
project.secondaire = form.cleaned_data['secondaire']
project.title_font = form.cleaned_data['title_font']
project.text_font = form.cleaned_data['text_font']
project.save()
messages.success(self.request, _('Project successfully created.'))
return redirect('projects:add_project')
add_project.html
{% extends 'layouts/default/page.html' %}
{% load bootstrap4 %}
{% load i18n %}
{% block content %}
<header id="page">
<div class="container">
<div class="table">
<div class="header-text">
<div class="row">
<div class="col-md-12 text-center">
<h1 class="white typed">{% trans 'Add project' %}</h1>
<span class="typed-cursor">|</span>
</div>
</div>
</div>
</div>
</div>
</header>
<section>
<div class="cut cut-top"></div>
<div class="container">
<div class="row content-tables">
<div id="error">
{% if error %}
<div class="alert alert-danger">
<i class="glyphicon glyphicon-warning-sign"></i>
{{error}}
</div>
{% endif %}
<!--{% if message %}-->
<!--<div class="alert alert-success">-->
<!--<i class="glyphicon glyphicon-warning-sign"></i>-->
<!--{{message}}-->
<!--</div>-->
<!--{% endif %}-->
</div>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form form %}
<button class="btn btn-success">{% trans 'Register your project' %}</button>
</form>
</div>
</div>
</section>
{% endblock %}
答案 0 :(得分:0)
在您的views.py中,
class classname(LoginRequiredMixin, View):
form_class = #addformname
template_name = #addtemplatename
def get(self, request):
---
def post(self, request):
form = self.form_class(request.POST, #add request.FILES if you are adding file)
if form.is_valid():
project = form.save(commit = False) #this is missing in your views
project.user = self.request.user
#add all your cleaned_data
project.save()
return redirect(#add the path)