-1

I'm stuck on creating form, looks like {{ forms.as_p }}. I need to login to website using this form. Uing forms.as_p it everything works perfectly, but I need to create my own customized form.

Problem is, that after reading lot's of website's and documentation I still can't understand, how to create custom form with my css class and input fields.

I tried this one:

action="", method ="post"
                     <div class="form-group">
                            <input type="text" class="form-control" placeholder="Username"  required="">
                        </div>
                        <div class="form-group">
                            <input type="password" class="form-control" placeholder="Password" required="">
                        </div>
<input type="submit" class="btn btn-primary block full-width m-b" value="Log In" />

But it doesn't work. What fields, I've missed.

Also I have another question: Is it possible to custom "next" variable to redirect after login on my custom page? And how I can do it?

D.Doe
  • 9
  • 8
  • The HTML snippet that you've posted isn't even complete. `action="", method ="post"` by itself means nothing. Please be clearer in what you are asking. – Pat Jones Nov 01 '17 at 18:21
  • 1
    Possible duplicate of [CSS styling in Django forms](https://stackoverflow.com/questions/5827590/css-styling-in-django-forms) – helb Nov 01 '17 at 18:21
  • Thx helb, but I still got one question. If I create form in forms.py and customize it, how should I call this form on my webpage? – D.Doe Nov 01 '17 at 18:45
  • @ return your form from view's context – Pushplata Nov 01 '17 at 18:53
  • Sorry, I'm newbie in django, still can't understand. – D.Doe Nov 01 '17 at 19:07

1 Answers1

0

Normally you use your forms.py to create a form that you can use in your HTML-Code via Jinja.

Your forms.py could look like this:

from django import forms

class ProfileForm(forms.Form):
    name = forms.CharField(label='Your name', max_length=100)
    age = forms.IntegerField(label='Your age')

Then you would do some cleaning to make sure the data passed to the form has the right format.

def clean_name(self):
    name = self.cleaned_data.get('name')
    if not name:
        raise forms.ValidationError('You have to type in a name.')
    return name

In your views.py:

def some_view(request):
      form = ProfileForm(request.POST or None)

      if form.is_valid():
            name = form.cleaned_data.get('name')
            //do something with the name
            user = User.objects.get(username=username)
            user.username = name
            user.save()
            return redirect('to_profile_of_user')
      return render(request, 'change_profile.html', {'form':form})

Here it is very important that you pass the form to your template(HTML).

Your change_profile.html:

<form action="" method="POST" enctype="multipart/form-data">
  {{form.as_p}} //That is the form you passed
  <input type="submit" value="Change" />
</form>

I hope my answer gives you a little bit of advice. Now you have seen a complete roundtrip. Maybe you should have a closer look at the Django documentation. It is very helpful.

The code is written on the fly and not tested. But it is enough for an overview. You should not forget to import the things you need. I did not do that.

Coder949
  • 987
  • 1
  • 8
  • 29
  • Thanks for expaining. I've the same thing as you, but without cleaning, and deleted name = form.cleaned.data.get ( still, don't understand why for is this row). But it doesn't work – D.Doe Nov 02 '17 at 09:34
  • `form.cleaned.data.get` is for getting the cleaned data back from your `forms.py`. What do you exactly want to know ? To know how to style your form with css ? – Coder949 Nov 02 '17 at 10:49