B
B
bwylla2019-02-27 14:14:34
Django
bwylla, 2019-02-27 14:14:34

How to overwrite the data entered by the user?

There is a form in which the user enters the first and last name, and then it is displayed on another page. I display the name and surname by the first and second id, but if you enter it several times, then in the first_name and last_name variables there will be a large list of different names and I need it to show exactly the last entered data.
I think that you need to somehow delete the old ones and write new names in their place, or somehow overwrite them, or in general it is done in a different way.
#forms.py

class UserProfilForm(forms.ModelForm):
    class Meta:
        model = UserProfil
        fields = ('first_name', 'last_name')

#models.py
class UserProfil(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)

#views.py
def user_profil(request):
    if request.method == 'POST':
        form = UserProfilForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('photo')
    else:
        form = UserProfilForm()
        return render(request, 'blog/index.html', {'form': form})

def list_user(request):
    return render(request, 'blog/photo.html', {'name': UserProfil.objects.get(id=1), 'name': UserProfil.objects.get(id=2)})

#index.html
<form method="post">
  {% csrf_token %}
  {{ form }}
  <input type="submit">
</form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Larisa .•º, 2019-02-27
@barolina

# может  это имеете ввиду 
def list_user(request):
     names =  UserProfil.objects.all().order_by('-id')[:2]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question