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/
Related
I'm not sure if I'm allowed to ask these types of questions here since it's not a problem per say, so please let me know. But I was wondering for the login.html template on Django:
{% extends 'learning_logs/base.html' %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method = "post" action="{% url 'login' %}">
{% csrf_token %}
{{ form.as_p }}
<button name = "submit">log in</button>
<input type="hidden" name="next" value="{% url 'learning_logs:index' %}" />
</form>
{% endblock content %}
How come it checks for the form.errors before the it even processes the form? Thanks for any help on this question.
The template ordering doesn't define how the form logic works, only how certain items are displayed. You could put the {% if form.errors %} block after the <form> and there would be no difference to how the form is processed by Django. Furthermore, form.errors will only exist once a form has been validated with is_valid() and any errors exist.
I created a blog like app. Whenever I update an entry and press the submit button, I want it to take me to view page for that blog entry. However it just redirects me to the update page. When I check form.errors it says that 'This field is required' for all of the fields. But all of the fields are filled with data.
{% extends 'base.html' %}
{% block content %}
{{ form.as_p }}
<form method="POST" action="">
{% csrf_token %}
<input type="submit" value="Update">
</form>
{% endblock content %}
You have your form declared in the wrong block:
{% extends 'base.html' %}
{% block content %}
<form method="POST" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update">
</form>
{% endblock content %}
Notice that the {{form.as_p}} has to be declared into the form block.
If you fill all data but your form is out of the form tag then the data will not be send.
I'm getting a conflict between editable fields and a mezzanine form (i.e. a standard form created in the admin).
When I make the page title 'editable', the form at the bottom renders a field to edit the title instead of the actual form. As the editable fields only show when you are logged in, this is only an issue when I am logged in.
If I remove 'editable' on the title the form renders as it should when logged in and logged out.
The code that causes the problem:
<h1>{% editable page.form.title %}{{ page.form.title }}{% endeditable %}</h1>
{% if request.GET.sent %}
<div class="well">
{% editable page.form.response %}
{{ page.form.response|richtext_filter|safe }}
{% endeditable %}
</div>
{% else %}
{% with page.form as page_form %}
{% editable page_form.content %}
{{ page_form.content|richtext_filter|safe }}
{% endeditable %}
<form method="post">
{% fields_for form %}
<input type="submit" value="{{ page.form.button_text }}">
</form>
{% endwith %}
{% endif %}
Any ideas very much appreciated :)
As discussed on IRC - there's a name collision in the templates, with a fix in Mezzanine pending.
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.
I have many forms which are working fine if i load them via normal http link.
The template is below
{% extends "app/base.html" %}
{% block title %}Create Account{% endblock %}
{% block media %} {% include "app/media_template.html" %} {% endblock %}
{% block heading %}Form{% endblock %}
{% block content %}
<div id="stylized" class="myform">
<form action="" method="post" enctype="multipart/form-data" >
<h1>Account form</h1>
<p>This is the basic look of my form without table</p>
{% csrf_token %}
{% for field in form %}
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% endfor %}
<button type="submit">Sign-up</button>
<div class="spacer"></div>
</form>
</div>
{% endblock %}
But if i have to display via ajax then i just need the div box containing form only , i don't nedd all other html
So i want that if JS is not working then those forms still work via hyperlink.
I have 6 forms , do i have to create seoarate templae if i call via ajax
If you need to use part of your template in several places, you can put that part in a separate template, and include it wherever you need:
{% include "foo/bar.html" %}
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#include