in Education by
tl;dr How to autofill an editable form with information stored in database Hey, Im creating a profile page for an application using Django as a framework. And Im having some annoying issues when a user is editing their page. As it is now, the user has to retype every field in the form, to edit a single field.. Cause my view has to delete the previous information in each field, or I get some annoying errors. So my question is, is there a way to autofill these fields in profile_edit.html with the strings corresponding to each field in the form, from the database? Any help would be greatly appreciated :D view.py @login_required def profile_edit(request): form = ProfileUpdateForm(request.POST, request.FILES) if request.method == 'POST': if form.is_valid(): user = request.user if 'image' in request.FILES: user.profile.image = request.FILES['image'] user.profile.bio = form.cleaned_data.get("bio") user.profile.birth_date = form.cleaned_data.get("birth_date") user.profile.location = form.cleaned_data.get("location") user.save() return redirect('profile') else: form = ProfileUpdateForm() context = { 'form' : form } return render(request, 'webside/profile_edit.html', context) models.py class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) bio = models.TextField(max_length=500, blank=True) location = models.CharField(max_length=30, blank=True) birth_date = models.DateField(null=True, blank=True) email_confirmed = models.BooleanField(default=False) image= models.FileField(upload_to='profile_image/', blank = True) def __str__(self): return self.user.username profile_edit.html '{% csrf_token %} {% for field in form %}

{{ field.label_tag }}
{{ field }} {% for error in field.errors %}

{{ error }}

{% endfor %} {% endfor %}' pic of profile.html forms.py class ProfileUpdateForm(forms.ModelForm): YEARS= [x for x in range(1900,2021)] birth_date = forms.DateField( initial="21-06-1995", widget=forms.SelectDateWidget(years=YEARS)) class Meta: model = Profile fields = ('bio','birth_date','location','image') JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The way you initialise your form in your view is all wrong: def profile_edit(request): user = request.user # form = ProfileUpdateForm(request.POST, request.FILES) <-- remove if request.method == 'POST': form = ProfileUpdateForm(request.POST, request.FILES, instance=user.profile) if form.is_valid(): form.save() # <-- you can just save the form, it will save the profile # user.save() <-- this doesn't help you, it doesn't save the profile and since user isn't changed you don't need to save it! return redirect(...) # else: # form = ProfileUpdateForm() <-- don't clear the form! else: # GET form = ProfileUpdateForm(instance=user.profile) <-- initialise with instance context = { 'form' : form } return render(request, 'webside/profile_edit.html', context) You need to add the instance to the form to update an existing instance. You shouldn't initialise an empty form if the form is not valid, because that means the user loses all the data if they made a mistake. You want to display the form with all the data and the errors in that case.

Related questions

0 votes
    I am a beginner at Django and I am attempting to create a custom table using a html template called ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    So, I started learning to code in Python and later Django. The first time it was hard looking at tracebacks and ... your Django code? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    In the Django model QuerySets, I see that there is a __gt and __lt for comparative values, but is there a __ne/!= ... (a=true, x__gt=5) Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    My Django app is unable to see MEDIA directory. When I run one of my views I need to open a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Is Django an important source to learn? Is it important in future? Can I grow with it? So questions: Can ... can work on Django? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to create a form that shows 2 different textboxes with a minimum and a maximum value of my ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I want to implement a simple 2 part FormWizard. Form 1 will by dynamically generated something like this: ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I have a very long forms.py and I'd like to split it to smaller parts with as few as possible ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    NOTE: I am not set on using VI, it is just the first thing that came to mind that might be able to do what I need ... there an automated way I can take the text: and change it to:...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    NOTE: I am not set on using VI, it is just the first thing that came to mind that might be able to do what I need ... there an automated way I can take the text: and change it to:...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    NOTE: I am not set on using VI, it is just the first thing that came to mind that might be able to do what I need ... there an automated way I can take the text: and change it to:...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    NOTE: I am not set on using VI, it is just the first thing that came to mind that might be able to do what I need ... there an automated way I can take the text: and change it to:...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I can use vi{ and va{ to select C++ code blocks. It helps me a lot when I need to yank ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I'm curious about manipulating time in Python. I can get the (last modified) age of a file using ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Pandas Merging 101 (6 answers) Closed 3 years ago. Sorry guys, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
...