0

Can you please help me

Forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User


class Registrationform(UserCreationForm):
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)
    mobile = forms.CharField(max_length=12, min_length=10)
    Business_name = forms.CharField(max_length=64)
    email = forms.EmailField(required=True)
    password2 = None

    class Meta:
        model = User
        fields = ['username', 'first_name', 'last_name', 'mobile', 'email', 'password1', 'Business_name']

    def save(self, commit=True):
        user = super(Registrationform, self).save(commit=False)
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.mobile = self.cleaned_data['mobile']
        user.email = self.cleaned_data['email']
        user.Business_name = self.cleaned_data['Business_name']

        if commit:
            user.save()
        return user

views.py

@login_required
def home(request):
    return render(request, 'users/home.html', {'user': request.user})

home.html

{% block content %}
    <p>
        {{ user.Business_name }}
        {{ user.first_name}}
        {{ user.last_name}}
        {{ user.email }}
        {{user.username}}
        {{ user.mobile }}
    </p>
{% end block %}

I don't understand why I am not able to fetch data even though doing the right thing.

Please help me as I am newbie in Django.

Quba
  • 4,776
  • 7
  • 34
  • 60
  • You try to receive data from a user model instance, not from the from. Take a look at this question https://stackoverflow.com/questions/4706255/how-to-get-value-from-form-field-in-django-framework – Quba Jun 26 '20 at 08:40

1 Answers1

0

Are you trying to access the form or models? if you want to submit form, you need to change your view to

def home(request):
if request.method == 'POST':
    form = ContactForm(request.POST)
    if form.is_valid():
        pass  # does nothing, just trigger the validation
else:
    form = ContactForm()
return render(request, 'home.html', {'form': form})

and add form tag to your home.html

 <form method="post" novalidate>
  {% csrf_token %}
  <table border="1">
    {{ form }}
  </table>
  <button type="submit">Submit</button>
</form>

I hope this helps!!