こんにちは私はdjangoで非常に新しいです。 1つのビューで異なるモデルを使用できるかどうかは疑問です。 だから私はこのページを見たhttps://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/ それはとても便利でしたが、私はブラウザのページで出力を見ることができませんでした。私はどこで間違いを犯したのですか?一つのビューで異なるモデル
私の見解
from .models import Contact, MyUser
from .forms import ContactForm, UserRegisterForm
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
def total_register(request):
uform = UserRegisterForm()
cform = ContactForm()
if request.method == "POST":
uform = UserRegisterForm(request.POST or None, instance=MyUser())
cform = [ContactForm(request.POST or None, prefix=str(x), instance=Contact()) for x in range(0,19)]
if uform.is_valid() and all([cf.is_valid() for cf in cform]):
new_MyUser = uform.save()
for cf in cform:
new_contact = cf.save(commit=False)
new_contact.MyUser = new_MyUser
new_contact.save()
return HttpResponseRedirect('/')
else:
pform = UserRegisterForm(instance=MyUser())
cform = [ContactForm(prefix=str(x), instance=Contact()) for x in range(0,19)]
return render_to_response('total_register.html', {'UserRegisterForm': uform, 'ContactForm': cform})
私のフォーム
class UserRegisterForm(forms.ModelForm):
username = forms.CharField(label = 'isminizi giriniz')
lastname = forms.CharField(label = 'soyisminizi giriniz')
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = [
'username',
'lastname',
]
def signup(self, request, user):
user.username = self.cleaned_data['username']
user.lastname = self.cleaned_data['lastname']
user.save()
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
fields = ['username','soyad','cinsiyet','unvan','kurum',"bolum","gorev", "uzmanlik","adres","posta_kodu","sehir","ulke","is_tel","ev_tel","fax_no","cep_tel","email"]
私のURL
url(r'^kayit/', bildirge.views.total_register, name = 'kayit'),
私のhtml
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class='col-sm-6 col-sm-offset-3'>
<h1>{{ title }}</h1>
<hr/>
<form method='POST' action='' enctype='multipart/form-data'>{% csrf_token %}
{{ form|crispy }}
<button type='submit'>gonder</button>
</form>
</div>
{% endblock content %}
ありがとうございます
ようこそ!あなたはDjangoの[formset](https://docs.djangoproject.com/en/1.9/topics/forms/formsets/) – Anzel
を使っても良いですか? – MaNKuR
私はこのビューのURLに行くと、私はボタンを送信する以外に何も見ません。それは私のフォームがそれではないことを示す必要がありますか? –