如何在一个视图中处理两种形式的数据? [英] How to handle data from two forms in one view?

查看:49
本文介绍了如何在一个视图中处理两种形式的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个模型的两个 forms.ModelForm

So I have two forms.ModelForm for my two models

第一:

class TranslatorChoice(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.user_id = kwargs.pop('user_id',None)
        super(TranslatorChoice, self).__init__(*args, **kwargs)
        self.fields['owner'].queryset = Translator.objects.all().filter(owner_id = self.user_id)
    owner = forms.ModelChoiceField(queryset =  None)
    class Meta:
        model = Translator
        fields = ('owner',)

第二:

class ProfileChoice(forms.ModelForm):
def __init__(self, *args, **kwargs):
    self.user_id = kwargs.pop('user_id',None)
    super(ProfileChoice, self).__init__(*args, **kwargs)
    self.fields['login'].queryset = Profile.objects.all().filter(created_by_id = self.user_id)

login = forms.ModelChoiceField(queryset= None, label='Profile')
class Meta:
    model = Profile
    fields = ('login',)

我尝试为他们编写一个 view ,但是它不起作用,似乎无法保存,因为每当我点击Submit按钮时,它都会刷新页面并清除字段,而不会将我重定向到所需的网址.我的数据库中的模型实例也未更新.

I've tried writing a view for them but it doesn't work, seems like it just won't save because whenever I hit submit button it just refreshes the page and cleans the fields without redirecting me to needed URL. The model instances in my DB aren't updated either.

这里是视图:

def link_profile(request):
context = {
'form': ProfileChoice(user_id=request.user.id),
'form2': TranslatorChoice(user_id=request.user.id)
}
if request.method == 'POST':
    form = ProfileChoice(request.POST)
    form2 = TranslatorChoice(request.POST)
    if form.is_valid():

        login = form.cleaned_data.get('login')
        translator = form.cleaned_data.get('owner')
        link = Profile.objects.get(login=login)
        link.owner = login
        link.save(['owner'])
        form.save()
        form2.save()
        return redirect('dashboard')
return render(request, 'registration/link.html', context)

我也知道有些问题是因为我习惯了许多 save 功能.我只是没有创建这样的 views 的经验...

I know also something is wrong is because I am using to many save functions. I just don't have any experience in creating views like that...

共享我的模板:

{% extends 'base.html' %}

{% block content %}
  <h2>Add profile</h2>
  <form method="post">
    {% csrf_token %}
    <table>

    {{ form.as_table }} {{ form2.as_table }}
    </table>

    <button type="submit">Link</button>
  </form>
{% endblock %}`

还有我的 urls.py 部分,并带有以下视图:

And my urls.py part with the view:

url(r'^link/', views.link_profile),

推荐答案

您没有共享模板中的urls.py或表单,因此不清楚是否正在执行视图或如何传递视图形式.但是,如果您还没有这样做,这可能会起作用.

You didn't share your urls.py or the form in your template so it's not clear if the view is being executed, or how you're passing your forms. But, here's something that might work if you're not doing it already.

仅供参考:您的代码存在一些缩进问题,但我认为那只是您在此处粘贴代码时发生的事情.

FYI: there are some indentation issues with your code but I'm assuming that that's just something that happened when you pasted it here.

这篇关于如何在一个视图中处理两种形式的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆