Django:表单未保存到数据库

Posted

技术标签:

【中文标题】Django:表单未保存到数据库【英文标题】:Django: Form is not saving to database 【发布时间】:2016-01-12 23:36:19 【问题描述】:

我在这里看到过类似类型的问题,但我仍然无法解决我的问题。当我在视图中保存 PhotographerProfileForm 时,表单会正确呈现,但在单击更新/提交后实际上并没有保存任何内容。

也没有显示错误。

我现在拥有的是预先填充的字段,我希望能够将这些字段保存在数据库中,但没有任何反应,目前您只能从管理面板更新它。


models.py

from __future__ import unicode_literals
from django.contrib.auth.models import User

from django.db import models


# Create your models here.
class PhotographerProfile(models.Model):
    user = models.OneToOneField(User)
    location = models.CharField(max_length=200)
    bio = models.TextField(max_length=500)

    def __str__(self):
        return self.user.username

User.profile = property(lambda u: PhotographerProfile.objects.get_or_create(user=u)[0])


from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=User)
def create_profile(sender,instance,created,**kwargs):
    if created:
        profile, new = PhotographerProfile.objects.get_or_create(user=instance)

urls.py

urlpatterns = [
url(r'^profile/$', 'photoprofile.views.photographer_profile', name = 'photographer_profile'),
url(r'^profile/portfolio/$', 'photoprofile.views.photographer_portfolio', name='photographer_portfolio'),

]


views.py

@login_required


def photographer_profile(request):
    photographerProfile = PhotographerProfile.objects.get(user=request.user)
    form = PhotographerProfileForm(initial='bio':photographerProfile.bio,     'location':photographerProfile.location)#This means you can prepoulate it
    return render_to_response('photographerprofile.html','form':form,  RequestContext(request))

    if request.method == 'POST':
        form = PhotographerProfileForm(request.POST, instance = request.user.profile,)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/profile')

    else:
        user = request.user
        profile = user.profile
        form = PhotographerProfileForm()

    return render(request, 'photographerprofile.html', 'form':form)

def photographer_portfolio(request):
    photographerProfile = PhotographerProfile.objects.get(user=request.user)
    return render_to_response('photographerportfolio.html', 'photographerProfile':photographerProfile, RequestContext(request))

forms.py

class PhotographerProfileForm(forms.ModelForm):

class Meta:
    model = PhotographerProfile
    exclude = ('user',)

photographerprofile.html

% extends 'base.html' %
% load crispy_forms_tags %

% block content %

<h2> Profile</h2>

% for field in form %
 field.error

% endfor %

<form action='/accounts/profile/' method='post'>
% csrf_token %
form|crispy

    <input type='submit' value='Update'/>

    <p>Click <a href='/accounts/profile/portfolio/'>here</a> to view portfolio.    </p>

</form>

% endblock %

【问题讨论】:

【参考方案1】:

在处理表单之前,您在视图的第三行返回。删除该行。

【讨论】:

返回 render_to_response('photographerprofile.html','form':form, RequestContext(request)) 。这条线?什么都没有发生 是的,完全删除该行。 是的,我已将其删除。它似乎没有做任何事情,它还删除了预先填充的数据。 尝试移动线来替换最终渲染。在return render_to_response('photographerprofile.html','form':form, RequestContext(request)) 之后移动第一个render_to_response return render_to_response('photographerprofile.html','form':form, RequestContext(request))。然后完全删除 return render_to_response('photographerprofile.html','form':form, RequestContext(request)) 您并没有通过完全删除它来提供 RequestContext。也请给我一分钟,我会检查其余的代码。 @AaronLayfield 抱歉,只是想知道您是否有机会查看它。花了一整天的时间重新启动,并出现同样的问题。我无法保存表单中的数据

以上是关于Django:表单未保存到数据库的主要内容,如果未能解决你的问题,请参考以下文章

页面未在 django 中保存表单数据

HTML表单数据未保存在数据库中 - django

Django 表单视图:M 到 M 值未保存

Django绑定表单无效,但未引发ValidationError

Django With MSSQL using Pyodbc:未保存模型表单

Django,模型表格未在数据库中保存数据