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.
Related
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 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/
I'm trying to create the views for all the authentication process in Django, but I have an issue with the reverse url tag in the views.
I have :
urls.py
url('^v1/back/', include('django.contrib.auth.urls'))
login.html
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<form method="post" action=".">
{{ form.as_p }}
<input type="submit" value="{% trans 'Log in' %}" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
<p>{% trans "Forgot password" %}? {% trans "Reset it" %}!</p>
<p>{% trans "Not member" %}? {% trans "Register" %}!</p>
{% endblock %}
And when I access localhost:8000/v1/back/login/ i have:
NoReverseMatch at /v1/back/login/
Reverse for 'django.contrib.auth.views.auth_password_reset' with
arguments '()' and keyword arguments '{}' not found. 0 pattern(s)
tried: []
The thing is, as, you can see, i try putting the complete path, it still does not work.
When I try to access another view like password change, it redirects me to :
http://192.168.56.103:8000/accounts/login/?next=/v1/back/password_change/
Which obviously does not work, and when i access password reset :
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<form method="post" action=".">
{{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" />
</form>
{% endblock %}
It has the django admin look and feel instead on my base.html.
I'm guessing there are lots of problem here, I'm trying to solve them one by one, but i don't know what i've done wrong on the url part.
Don't know if this is relevant, but a part of my application is served by Django Rest Framework, and i'm trying to put in place oAuth2.
The correct name for password reset is just password_reset, to reverse it use:
{% url 'password_reset' %}
To fix the login redirect, you have to adjust the LOGIN_URL in your settings.py
I suggest you remove the part regarding the template and post it as a separate question.
I have a form involving just two textareas and I'm trying to POST them. However, I'm having difficulty trying to get the POST data from my view. Here's what I have so far in my template:
{% block title %}
<form method="post">
<strong>Title:</strong>
<div>
<textarea contenteditable id="id-title" name="title"></textarea>
</div>
{% endblock title %}
{% block description %}
<strong>Description:</strong>
<div>
<textarea contenteditable id="id-description" name="description"></textarea>
</div>
</form>
{% endblock description %}
<button class="btn-primary" id="ok" url="{% url 'publish' id=id %}" type="submit">Publish</button>
view:
#ajax
#csrf_exempt
#admin_required
def publish(request, id):
title = request.POST.get('title')
desc = request.POST.get('description')
print title
print desc
...
But title and desc turn out to be None. My publish function is also an ajax function that does some other stuff when the button is pressed.
Try altering your form with action, and moving your submit inside the form, e.g.:
<form method="post" action="{% url 'publish' id=id %}">
{% block title %}
<strong>Title:</strong>
<div>
<textarea contenteditable id="id-title" name="title"></textarea>
</div>
{% endblock title %}
{% block description %}
<strong>Description:</strong>
<div>
<textarea contenteditable id="id-description" name="description"></textarea>
</div>
{% endblock description %}
<input id="ok" type="submit">
</form>
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>