django-registration-redux and django-crispy-forms: submit button not responding - django

my registration_form.html is
{% extends '../base.html' %}
{% load crispy_forms_tags %}
{% block content %}
{% crispy form %}
<input type='submit' value="submit">
{% endblock %}
the view is in a module in an existing app. its below:
from registration.views import RegistrationView
from . import forms
class MyRegistrationView(RegistrationView):
def get_form_class(self):
return forms.LoginForm
the url in the urls.py file is
url(r'^accounts/register/$',MyRegistrationView.as_view()),
when the page load, on filling it, when i click submit, it does not submit.
pls what am i missing?

It looks as if your form is not enclosed in a <form> tag. You should have something like:
<form method="post" action="">
{% crispy form %}
<input type='submit' value="submit">
</form>

Related

How to arrange forms fields vertically? [duplicate]

This question already has answers here:
How to change form layouts in Django 1.8
(6 answers)
Closed last year.
I am creating a login form by using the Forms class. Unfortunately, when I run the page I found the arrangement of the fields is horizontal. Can you help me to make it vertically, please?
In the bellow the forms.py code:
from django import forms
class LoginForm(forms.Form):
username = forms.CharField(label='Your name', max_length = 50)
password = forms.CharField(max_length = 50, widget=forms.PasswordInput)
And here is the views.py code:
from django.shortcuts import render
from .models import Login
from .forms import LoginForm
# Create your views here.
def login_function(request):
try:
username = request.POST.get('username')
password = request.POST.get('password')
data = Login(username = username, password = password)
data.save()
except:
x = 'error'
return render(request,'pages/login.html', {'loginform':LoginForm})
And here is login.html code:
{% block content %}
<form method="POST">
{% csrf_token %}
{{loginform}}
<input type="submit" value="Save">
</form>
{% endblock content %}
First Way using as_p
{% block content %}
<form method="POST">
{% csrf_token %}
{{loginform.as_p}}
<input type="submit" value="Save">
</form>
{% endblock content %}
Second Way using loop
If you want to add any additional styles to your form, then
{% block content %}
<form method="POST">
{% csrf_token %}
{% for field in loginform%}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }} <br/>
{{ field }}
</div>
{% endfor %}
<button type="submit">Save</button>
</form>
{% endblock %}
Third-way using table
{% block content %}
<form method="POST">
{% csrf_token %}
<table>
{{ loginform.as_table }}
</table>
<button type="submit">Save</button>
</form>
{% endblock %}

how to pass a primary key to a view in django?

I am trying to implement a class based view that should create a update form to update my model form but I don not know how to pass the pk from my base.html to my view:
viewvs.py:
from artdb.models import *
class UpdateForm(UpdateView):
print('updateform')
model=Date
fields=['activity']
template_name='updateForm.html'
updateForm.html:
{% extends "artdb/base.html" %}
{% block upd %}
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update">
</form>
{% endblock upd %}
base.html:
<p><a class="btn btn-secondary" href="{% url 'artdb:updateform' %}" role="button">update form ยป</a></p
{% block upd %}
{% endblock upd %}
urls.py:
urlpatterns = [
path('<pk>/updateform/',views.UpdateForm.as_view(),name='updateform'),
]
I think that pk should be passed in the base.html but I am not shure how. Any suggestions?
I think you've probably got it by now, but if you didn't, try it like this in the app url.py file:
urlpatterns = [
path('updateform/<int:pk>',views.UpdateForm.as_view(),name='updateform'),
]

Django Registration Registration_form

I'm using Django Registration_redux templates for user registrations.
I am wondering how does Django know where to go when user submits his registration when action is action="." ?
{% extends "registration/registration_base.html" %}
{% load i18n %}
{% block title %}{% trans "Register for an account" %}{% endblock %}
{% block content %}
<form method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" />
</form>
{% endblock %}
{% comment %}
**registration/registration_form.html**
Used to show the form users will fill out to register. By default, has
the following context:
``form``
The registration form. This will be an instance of some subclass
of ``django.forms.Form``; consult `Django's forms documentation
<http://docs.djangoproject.com/en/dev/topics/forms/>`_ for
information on how to display this in a template.
{% endcomment %}
If your action is blank or . then the post goes to the view (URL) which rendered the form.
Here's a simple example using a contact form; https://hellowebapp.com/news/tutorial-setting-up-a-contact-form-with-django/

Error loading sample django-bootstrap3 template

I'm still new to Django and Bootstrap so I'm trying out the django-bootstrap package: https://github.com/dyve/django-bootstrap3
The sample template that is included on that page (with a change of url in form action):
{% load bootstrap3 %}
{# Load CSS and JavaScript #}
{% bootstrap_css %}
{% bootstrap_javascript %}
{# Display a form #}
<form action="/search/" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% bootstrap_form_buttons %}
<button type="submit" class="btn btn-primary">
{% bootstrap_icon "star" %} Submit
</button>
{% end_bootstrap_form_buttons %}
</form>
Gives me the error:
BootstrapError at /
Parameter "form" should contain a valid Django Form.
on this line
{% bootstrap_form form %}
I'm not exactly sure what's the problem is since this is the sample template that's included in that README.
{% bootstrap_form form %} is a template tag provided by django-bootstrap3 that expect a django form instance, so the "form" parameter is the context variable mentioned in displaying-a-form-using-a-template from django docs.
Create the form as explained in that page, and then replace the html code they use in template:
<form action="/contact/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
by the sample code in your question.
Now Parameter "form" contains a valid Django Form
Hope this helps
You just need to provide an object form server side, which must have a context name "form".
In your views.py, include something like this
from django.shortcuts import render
def index(request):
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
template = "your_template.html"
context = { "form" : NameForm() }
return render( request, template, context )
Now you should not have any error.
Hope it helps
try this
{# Load the tag library #}
{% load bootstrap3 %}
{# Load CSS and JavaScript #}
{% bootstrap_css %}
{% bootstrap_javascript %}
{# Display django.contrib.messages as Bootstrap alerts #}
{% bootstrap_messages %}
{# Display a form #}
<form action="/url/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-primary" value="Submit" />
</form>
You don't really need the bootstrap_button tags. I tried to find them as well, but they are not declared in the source...
Put {% extends 'bootstrap3/bootstrap3.html' %} at the beginning of your snippet. It's supposed to be your file, bootstrap3.html is at this placeholder.
Error is pretty straightforward, make sure you pass valid django form. I was passing form.as_p() instead of form in my view and got this error. Took me a while to notice. May be it will still help somebody.
for django 1.8 use {{ form }} instead of {{ form.as_p }} as in django 1.6, for this minor change can cause an error
please see django 1.8 official documentation:
https://docs.djangoproject.com/en/1.8/topics/forms/#the-template
{% load bootstrap3 %}
{# Display a form #}
<form action="/submit/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>

Page stall when passing data

I am trying to pass some data between pages, but it's not working. Any tips? I click submit and it takes me to a blank page. If I refresh then it shows my base template styles, but no data is passed.
index.html
{% extends "polls/base.html" %}
{% block title %}Vote{% endblock %}
{% block content %}
<h1>Welcome</h1>
<form action="/polls/" method="post">{% csrf_token %}
<p><label for="pin">Enter group pin:</label>
<input id="pin" type="text" name="pin" maxlength="4" />
<input type="submit" value="View Polls" /></p>
</form>
Moderator login
</p>
{% endblock %}
polls/index.html
{% extends "polls/base.html" %}
{% block title %}Recent Polls{% endblock %}
{% block content %}
{{ pin }}
{% endblock %}
polls/urls.py
url(r'^$',
ListView.as_view(
model=Poll,
template_name='polls/index.html')),
You need to write a view to handle the form submision. The view that works, is listing all your Polls. I recommend you to read the tutorial:
Basic views: https://docs.djangoproject.com/en/1.4/intro/tutorial03/
Form submission: https://docs.djangoproject.com/en/1.4/intro/tutorial04/
Basically, your form will send the data to another URL that will handle the processing of your data.
You must specify it in the action attribute:
<form action="/polls/create-poll" method="post">{% csrf_token %}
<input type='text' name='poll-name' />
<input type='submit' />
</form>
and in your views:
def create_poll(request):
poll_name = request.POST.get('poll-name')
poll = Poll.objects.create(name=poll_name)
return HttpResponse("Poll created")
I don't want to sound rude. But you should start with some HTTP and HTML tutorial. A good web programmer is the one that knows the basic stuff in detail. HTTP is a great protocol, try to learn it all the way through.