现在已经读了好几个小时了。仍然不知道这是如何工作的。 我试图在django中验证一个简单的用户配置文件表单,但该函数总是返回 - 嗯 - 没什么(?)...即使我尝试打印出form.is_valid()也没有返回。
这里是代码,也许有人有任何想法:
FORMS.PY
class UserForm(ModelForm):
class Meta:
model = User
fields = ['username', 'password', 'email', 'first_name', 'last_name']
def __init__(self, instance):
super(UserForm, self).__init__()
self.fields['username'].widget.attrs['readonly'] = True
self.fields['password'].widget = forms.PasswordInput()
for field in self.fields:
self.fields[field].widget.attrs['class'] = 'form-control'
self.fields[field].widget.attrs['value'] = getattr(instance, field)
class UserDataForm(ModelForm):
class Meta:
model = UserProfile
fields = ['picture', 'academicDiscipline', 'studentNumber', 'location']
def __init__(self, instance):
super(UserDataForm, self).__init__()
for field in self.fields:
if field != 'picture':
self.fields[field].widget.attrs['class'] = 'form-control'
self.fields[field].widget.attrs['value'] = getattr(instance, field)
VIEWS.PY
curUser = User.objects.get(pk = request.user.id)
curUserProfile = curUser.userprofile
success_msg, error_msg = None, None
if request.method == 'POST':
user = User.objects.get( username__exact = request.POST['username'] )
userProfile = None
user.email = request.POST['email']
user.first_name = request.POST['first_name']
user.last_name = request.POST['last_name']
userForm = UserForm(user)
if userForm.is_valid():
user = userForm.save()
user.save()
success_msg = 'Benutzerdaten wurden erfolgreich aktualisiert.'
userProfile = user.userprofile
userProfile.academicDiscipline = request.POST['academicDiscipline']
userProfile.studentNumber = request.POST['studentNumber']
userProfile.location = request.POST['location']
userDataForm = UserDataForm(userProfile)
if userDataForm.is_valid():
userDataForm.save()
userDataForm = userDataForm
success_msg = 'Benutzerdaten wurden erfolgreich aktualisiert.'
else:
error_msg = "userProfileData failure"
else:
error_msg = 'userData failure'
context = {
'active_page' : 'settings',
'nav': Nav.nav,
'success_msg': success_msg,
'error_msg': error_msg,
'userForm': UserForm(curUser),
'userDataForm': UserDataForm(curUserProfile)
}
return render(request, 'settings.html', context)
else:
context = {
'active_page' : 'settings',
'nav': Nav.nav,
'userForm': UserForm(curUser),
'userDataForm': UserDataForm(curUserProfile)
}
return render(request, 'settings.html', context)
SETTINGS.HTML
<form action="{% url 'twittur:settings' %}" method="post">
{% csrf_token %}
{{ userForm.non_field_errors }}
{{ userDataForm.non_field_errors }}
<div class="post" id="profilbild">
<h4>Profilbild auswählen</h4>
<div class="col-xs-12 form-group">
<div class="col-xs-12 text-center" style="vertical-align: bottom !important;">
<img src="/picture/{{ user.picture }}" class="img-thumbnail" style="width: 100px;">
<img src="/picture/{{ user.picture }}" class="img-thumbnail" style="width: 75px; margin-top: 26px;">
<img src="/picture/{{ user.picture }}" class="img-thumbnail" style="width: 50px; margin-top: 50px;">
<p class="help-block">Aktuelles Profilbild (100px, 75px, 50px). {{ user.picture }}</p>
</div>
<label class="form-label" for="avatarInput">Profilbild aktualisieren</label>
{{ userDataForm.picture }}
{{ userDataForm.picture.errors }}
<!--<input id="avatarInput" name="picture" type="file">-->
</div>
</div>
<div class="post" id="account">
<h4>Accountverwaltung</h4>
<div class="col-xs-12 form-group">
<label class="control-label" for="usernameInput">Username</label>
<!--<input name="userID" type="hidden" value="{{ user.id }}" />-->
{{ userForm.username }}
<!--<input class="form-control" id="usernameInput" name="username" type="text" value="{{ user.username }}" disabled />-->
<p class="help-block">Der Username kann nicht verändert werden.</p>
{{ userForm.username.errors }}
</div>
<div class="col-xs-12 form-group">
<label class="control-label" for="emailInput">E-Mail</label>
{{ userForm.email }}
{{ userForm.email.errors }}
<!--<input class="form-control" id="emailInput" name="email" type="email" value="{{ user.email }}" />-->
</div>
<div class="col-xs-12 col-sm-6 form-group">
<label class="control-label" for="pw1Input">Passwort</label>
{{ userForm.password }}
{{ userForm.password.errors }}
<!--<input class="form-control" id="pw1Input" name="password" type="password" value="{{ user.password }}" />-->
</div>
<div class="col-xs-12 col-sm-6 form-group pull-right">
<label class="control-label" for="pw2Input">Passwort bestätigen</label>
{{ userForm.password.errors }}
<!--<input class="form-control" id="pw2Input" type="password" value="{{ user.password }}" />-->
</div>
</div>
<div class="post" id="daten">
<h4>Persönliche Daten</h4>
<div class="col-xs-12 col-sm-6 form-group">
<label class="control-label" for="vornameInput">Vorname</label>
{{ userForm.first_name }}
{{ userForm.first_name.errors }}
<!--<input class="form-control" id="vornameInput" name="vorname" type="text" value="{{ user.first_name }}" />-->
</div>
<div class="col-xs-12 col-sm-6 form-group pull-right">
<label class="control-label" for="nachnameInput">Nachname</label>
{{ userForm.last_name }}
{{ userForm.last_name.errors }}
<!--<input class="form-control" id="nachnameInput" name="nachname" type="text" value="{{ user.last_name }}" />-->
</div>
<div class="col-xs-12 form-group">
<label class="control-label" for="studiengangInput">Studiengang</label>
{{ userDataForm.academicDiscipline }}
<!--<input class="form-control" id="studiengangInput" name="studiengang" type="text" value="{{ user.academicDiscipline }}" />-->
<p class="help-block">Über deinen Studiengang wirst Du bestimmten Gruppen zugeordnet.</p>
{{ userDataForm.academicDiscipline.errors }}
</div>
<div class="col-xs-12 form-group">
<label class="control-label" for="matrikelNummerInput">Matrikel-Nummer</label>
{{ userDataForm.studentNumber }}
<!--<input class="form-control" id="matrikelNummerInput" name="matrikelnummer" type="number" value="{{ user.studentNumber }}" />-->
<p class="help-block">Über deine Matrikel-Nummer kannst Du eindeutig als Student der TU Berlin identifiziert werden.</p>
{{ userDataForm.studentNumber.errors }}
</div>
<div class="col-xs-12 form-group">
<label class="control-label" for="wohnortInput">Wohnort</label>
{{ userDataForm.location }}
{{ userDataForm.location.errors }}
<!--<input class="form-control" id="wohnortInput" name="wohnort" type="text" value="{{ user.location }}" />-->
</div>
</div>
<div class="post" id="seite">
<h4>Seiteneinstellungen</h4>
<div class="col-xs-12 form-group">
<label class="control-label" for="stdSafetylevelSelect">Standard-Sicherheitsstufe</label>
<select class="form-control" id="stdSafetylevelSelect" name="safety">
<option>public</option>
<option>Uni</option>
<option>Fakultät</option>
<option selected>Studiengang</option>
</select>
</div>
</div>
<div class="post" id="delete">
<h4>Account vollständig löschen</h4>
<div class="col-xs-12 form-group text-center">
<p class="help-block">Über diesen Button kannst Du deinen Account vollständig von unserer Plattform entfernen. Zur Bestätigung der Löschung erhälst Du eine E-Mail an deine uns bekannte E-Mail Adresse.</p>
<button type="button" class="btn btn-primary text-center" id="deleteButton">Account löschen</button>
</div>
</div>
<div class="col-xs-12 form-group text-center">
<button type="submit" class="btn btn-primary" id="submitSettButton">Speichern</button>
<button type="reset" class="btn btn-default" id="resetSettButton">Zurücksetzen</button>
</div>
</form>
我完全不知道如何处理它。所以我会感激各种帮助!
招呼
答案 0 :(得分:1)
您永远不会将POST数据传递给表单:因此它保持未绑定状态,并且不会通过验证。您需要执行userDataForm = UserDataForm(request.POST, instance=userProfile)
更糟糕的是,您已明确定义了表单'__init__
方法,以便它们甚至不接受除实例之外的任何参数 - 因此不可能随后填充数据。另外,它们实际上吞下了instance
参数,并且没有将它传递给超类,因此它实际上被忽略了。您应该像这样定义它们:
def __init__(self, *args, **kwargs):
instance = kwargs.get('instance')
super(UserDataForm, self).__init__(*args, **kwargs)
答案 1 :(得分:0)
您需要通过直接将request.POST传递给视图来实例化userForm - 您正在传递用户模型实例。
尝试: .data
msg1: .asciiz "> "
msg2: .asciiz "type 'c' to copy \n>"
.align 2
array: .space 400
.text
main:
la $t3 array
loop:
la $a0, msg1 #output message 1
li $v0, 4
syscall
li $v0, 5 #read integer input
syscall
move $t0, $v0
beq $t0, -99, endloop #loop until user types -99
beq $t1,30,endloop #get user input up to 30 times
addi $t1, $t1, 1 #counter
sw $t0,($t3)
addi $t3,$t3,4
b loop #loop until it reaches 30
endloop:
la $a0, msg2 #output message 2
li $v0, 4
syscall
li $v0, 12 #read character input
syscall
beq $v0, 'c', COPY
j NEXT
COPY:
NEXT: