Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
How to pass user object to forms in Django

How would I pass a user object or a request to my form for using it as an initial value for input textbox?

For example, I have my form:

class ContactForm(forms.Form):
contact_name = forms.CharField(required=True, initial="???")
contact_email = forms.EmailField(required=True)
subjects = forms.ChoiceField(choices=emailsubjects)
content = forms.CharField(
    required=True,
    widget=forms.Textarea
)

def __init__(self, *args, **kwargs):
    self.request = kwargs.pop("request")
    super(ContactForm, self).__init__(*args, **kwargs)
    self.fields['contact_name'].label = "Your name:"
    self.fields['contact_email'].label = "Your email:"
    self.fields['content'].label = "What do you want to say?"
    self.fields['subjects'].label = "Please, select the subject of your message"

Where i want my user.first_name to be as a default value for contact_name field.

Here is my views.py, where i call for form:

def ContactsView(request):
form_class = ContactForm(request=request)
# new logic!
if request.method == 'POST':
    form = form_class(data=request.POST)

    if form.is_valid():
        contact_name = request.POST.get(
            'contact_name'
            , '')
        contact_email = request.POST.get(
            'contact_email'
            , '')
        form_content = request.POST.get('content', '')
        subjects = form.cleaned_data['subjects']
        subjects = dict(form.fields['subjects'].choices)[subjects]
        # Email the profile with the
        # contact information
        template = get_template('threeD/email/contact_template.txt')
        context = Context({
            'contact_name': contact_name,
            'subjects': subjects,
            'contact_email': contact_email,
            'form_content': form_content,
        })
        content = template.render(context)

        email = EmailMessage(
            "New message from " + contact_name,
            content,
            "Message - " + subjects + ' ',
            ['smart.3d.printing.facility@gmail.com'],
            headers={'Reply-To': contact_email}
        )
        email.send()
        messages.success(request, "Thank you for your message.")
        return redirect('/index/contacts/')


return render(request, 'threeD/contacts.html', {
    'form': form_class,
})

Any help would be very much appreciated

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You have changed your form to take the request object. Therefore you can access self.request.user inside your form's methods:

class ContactForm(forms.Form):
    ...
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        super(ContactForm, self).__init__(*args, **kwargs)
        self.fields['contact_name'].label = "Your name:"
        self.fields['contact_name'].initial = self.request.user.first_name

You also have to update your view to pass the request object. Remember to update the code for GET and POST requests.

if request.method == 'POST':
    form = ContactForm(data=request.POST, request=request)
    ...
else:
    # GET request
    form = ContactForm(request=request)

Finally, by passing the request to the form, you have tightly coupled it to the view. It might be better to pass the user to the form instead. This would make it easier to test the form separately from the view. If you change the form, remember to update the view as well.

about 4 years ago · Santiago Trujillo Denunciar

0

You need to pass the initial values in the view:

views:

def ContactsView(request):
    form_class = ContactForm(request=request,
                             initial={'contact_name': request.user.first_name})
    ...
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda