python - Django - User Profile field won't change after form submit -
i'm trying have part of user profile change after form submitted. user profile has discipline field, , want user able modify it. when click submit now, nothing changes.
i beginner @ django, sure minor fix. i've been trying work past few days.
views.py
@login_required def change_discipline(request): context = requestcontext(request) if request.method == 'post': # #create form instance , populate data request form = disciplinechoiceform(request.post) if form.is_valid(): #process data in form.clean_data discipline = discipline(name=form.cleaned_data['discipline']) request.user.profile.primarydiscipline = discipline request.user.save() request.user.profile.save() return render_to_response('changediscipline.html', { 'form': form }, context) else: form = disciplinechoiceform(request.post) return render_to_response('changediscipline.html', {'form': form}, context )
models.py user profile
class userprofile(models.model): #this line required. links myuser user model user = models.onetoonefield(user, related_name ="profile") #additional attributes wish include date_of_birth = models.floatfield(blank=false) phone = models.charfield(max_length=10, blank = true) city = models.charfield(max_length=40, blank = true) state = models.charfield(max_length=2, blank = true) zipcode = models.charfield(max_length=5, blank = true) admin = models.booleanfield(default=false, blank = true) mentor = models.booleanfield(default=false, blank = true) mentee = models.booleanfield(default=false, blank = true) # profilepicture = models.imagefield() #is_staff = true tagline = models.charfield(max_length=70, blank = true, default="let's this!") interests = models.manytomanyfield(interest, related_name="interest", symmetrical = false) primarydiscipline = models.foreignkey(discipline, default=false, blank = true) addtldisciplines = models.manytomanyfield(discipline, related_name="disciplines", symmetrical=false)
html
<div class = "container"> <h1>choose discipline in {{interest}}</h1> <form action="{% url 'myapp:change_discipline' %}" method="post" id="discipline-select"> {% csrf_token %} {{ form }} <input type="submit" value="submit" /> </form> <!-- {% csrf_token %} <select id="id_interest" name="discipline"> <option disabled selected> -- select option -- </option> {% d in disciplines %} <option value={{d.id}}>{{d.name}}</option> {% endfor %} </select> <input type="submit" value="load disciplines"/> </form> --> </div>
forms.py
class disciplinechoiceform(forms.form): def __init__(self, interest, *args, **kwargs): super(disciplinechoiceform, self).__init__(*args, **kwargs) self.fields['discipline'] = forms.choicefield(choices = [(o.id, str(o)) o in discipline.objects.all()])
ok should have read better. first problem:
# creates unsaved discipline instance discipline = discipline(name=form.cleaned_data['discipline']) # , assign request.user.profile request.user.profile.primarydiscipline = discipline
since profile.primary_discipline
allows nulls, call request.user.profile.save()
doesn't raise integrityerror, nothing happens indeed.
now didn't post disciplinechoiceform
don't know form.cleaned_data['discipline']
points to, that's not going work - want actual (saved) discipline
instance.
if form's discipline
field forms.choicefield
, has (id, name)
tuples choices, form.cleaned_data['discipline']
yield discipline id, , you'll correct discipline
instance discipline.objects.get(id=form.cleaned_data['discipline'])
:
discipline = discipline.objects.get(id=form.cleaned_data['discipline']) request.user.profile.primarydiscipline = discipline request.user.profile.save()
but might better using forms.modelchoicefield
instead directly return selected discipline
instance in case can simplify code to:
discipline = form.cleaned_data['discipline'] request.user.profile.primarydiscipline = discipline request.user.profile.save()
Comments
Post a Comment