I write a message board,When user leave message and success back to the page
I want to alert('Thank you') to let user know it's success.
But the message didn't show
I add this in my setting ,still not work
TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.messages.context_processors.messages",)
Please help me ,Thank you .
views.py
def maininfo(request):
return render(request, 'zh_tw/maininfo.html',)
def create_post(request):
if request.method == 'POST':
form = MessageForm(request.POST)
if form.is_valid():
form.save()
messages.success(request,'Thank you')
return HttpResponseRedirect('/maininfo/#5thPage')
return render(request, "zh_tw/maininfo.html",{'form': form,'anchor':'#5thPage'})
urls.py
urlpatterns = patterns('',url(r'^maininfo/$', views.maininfo, name='maininfo'),)
template: zh_tw/contact.html
(this is an anchor page included by zh_tw/maininfo.html)
{% if messages %}
{% for message in messages %}
<p{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</p>
{% endfor %}
{% endif %}
<form action="{% url 'core:create_post' %}" method="POST" id="create_post">
{% csrf_token %}
{% if not form %}
<div>
<input type="text" name="name" id="name" placeholder="你的名字">
<input type="text" name="email" id="email" placeholder="你的電子信箱">
</div>
<textarea placeholder="你想說的話" name = "message" id="message"></textarea>
<br>
<button type="submit" value="Submit" id="submit">送出</button>
{% endif %}
{% if form %}
{% if form.errors %}
<div>
<span style="color:red ; font-weight:bold">
{{ form.name.errors }} {{ form.email.errors }} {{ form.message.errors }} </span>
<input type="text" name="name" id="name" placeholder="你的名字" value="{{form.name.value }}" >
<input type="text" name="email" id="email" placeholder="你的電子信箱" value="{{form.email.value}}">
</div>
<textarea placeholder="你想說的話" name = "message" id="message">{{form.message.value}}</textarea>
<br>
<button type="submit" value="Submit" id="submit">送出</button>
{% endif %}
{% endif %}
</form>
You have to add the same message block in the redirection page, return HttpResponseRedirect('/maininfo/#5thPage')
the message will be displayed normaly !
Related
I have three diffrent apps in my django project, Home, jobseeker and recruiter. and each app have different subdomain. Home => localhost:8000, jobseeker => jobseeker.localhost.8000, and for recruiter => recruiter.localhost:8000. i have an registration form in my Home app(localhost:8000) when i submitting the form i am getting..
Forbidden (403)
CSRF verification failed. Request aborted.
Help
Reason given for failure:
Origin checking failed - null does not match any trusted origins.
how can i solve this?
here is my hosts.py file
from django_hosts import patterns, host
host_patterns = patterns('',
host(r'^(?:www\.)?$', 'Home.urls', name='perfectpesha'),
host(r'jobseeker', 'jobseeker.urls', name='jobseeker'),
host(r'recruiter', 'recruiter.urls', name='recruiter'),
)
here is my registration form
{% extends "layout.html" %}
{% load i18n %}
{% load hosts %}
{% load static %}
{% load tz %}
{% block content %}
<form class="form-horizontal" method="POST" action="{% host_url "jobseekerRegister" host "jobseeker" %}">
{% csrf_token %}
<div class="formrow">
<input type="text" name="name" class="form-control" required="required"
placeholder="Full Name" {% if form.name.errors %}style="border: 1px solid #FE0004"{% endif %}>
{% if form.name.errors %}
{% for error in form.name.errors %}
<span class="help-block-error"> <strong>{{ error }}</strong></span>
{% endfor %}
{% endif %}
</div>
<div class="formrow">
<input type="email" name="email" class="form-control" required="required"
placeholder="Email" {% if form.email.errors %}style="border: 1px solid #FE0004"{% endif %}>
{% if form.email.errors %}
{% for error in form.email.errors %}
<span class="help-block-error"> <strong>{{ error }}</strong></span>
{% endfor %}
{% endif %}
</div>
<div class="formrow">
<input type="number" name="mobile" class="form-control" required="required"
placeholder="Mobile" {% if form.mobile.errors %}style="border: 1px solid #FE0004"{% endif %}>
{% if form.mobile.errors %}
{% for error in form.mobile.errors %}
<span class="help-block-error"> <strong>{{ error }}</strong></span>
{% endfor %}
{% endif %}
</div>
<div class="formrow">
<input type="password" name="password" class="form-control" required="required"
placeholder="Password" {% if form.password.errors %}style="border: 1px solid #FE0004"{% endif %}>
{% if form.password.errors %}
{% for error in form.password.errors %}
<span class="help-block-error"> <strong>{{ error }}</strong></span>
{% endfor %}
{% endif %}
</div>
<div class="formrow">
<input type="password" name="password_confirmation" class="form-control"
required="required" placeholder="Password Confirmation" {% if form.password_confirmation.errors %}style="border: 1px solid #FE0004"{% endif %}>
{% if form.password_confirmation.errors %}
{% for error in form.password_confirmation.errors %}
<span class="help-block-error"> <strong>{{ error }}</strong></span>
{% endfor %}
{% endif %}
</div>
<div class="formrow">
<textarea class="form-control" name="address" placeholder="address"></textarea>
</div>
<div class="formrow">
<input type="checkbox" value="1" name="is_subscribed"
checked="checked"/>
Subscribe to Newsletter
</div>
<div class="formrow">
<input type="checkbox" value="1" name="terms_of_use"/>
I accept Terms of Use
</div>
<input type="submit" class="btn" value="Register">
</form>
{% endblock %}
and here is jobseeker url
path("register/", views.register, name="jobseekerRegister"),
my form is submitting from localhost:8000/login to jobseeker.localhost:8000/login but i am getting csrf error.
this is my settings.py file
ALLOWED_HOSTS = [env('HOST'), 'jobseeker.' + env('HOST'), 'recruiter.' + env('HOST')]
CSRF_TRUSTED_ORIGINS = ['http://' + env('HOST'), 'http://jobseeker.' + env('HOST'), 'http://recruiter.' + env('HOST')]
in .env file
HOST='localhost'
please help me.....
Hello I use the allauth package to validate forms. The problem I have in this case is that I want to change the default templates of the allauth package and add my own styles and css classes to them. And I have done this, but my problem is that when the user enters incomplete or incorrect login information, no validation message is displayed to the user. I want to change the default allauth templates, such as login.html, signup.html, etc. and also I want to display my own authentication messages. Please guide step by step I because am learning Django, Thanks.
This is login.html
{% extends "base.html" %}
{% load i18n %}
{% load account socialaccount %}
{% block head_title %}{% trans "Sign In" %}{% endblock %}
{% block content %}
<h1 class="head-line">{% trans "Sign In" %}</h1>
{% get_providers as socialaccount_providers %}
{% if socialaccount_providers %}
<div class="container">
<p class="text-center">{% blocktrans with site.name as site_name %}Please sign in with one
of your existing third party accounts. Or, sign up
for a {{ site_name }} account and sign in below:{% endblocktrans %}
</p>
</div>
<div class="socialaccount_ballot container">
<ul class="socialaccount_providers text-center border">
{% include "socialaccount/snippets/provider_list.html" with process="login" %}
</ul>
<div class="login-or text-center mt">{% trans 'or' %}</div>
</div>
{% include "socialaccount/snippets/login_extra.html" %}
{% else %}
<p>{% blocktrans %}If you have not created an account yet, then please
sign up first.{% endblocktrans %}</p>
{% endif %}
<div class="container">
<form class="login" method="POST" action="{% url 'account_login' %}">
{% csrf_token %}
<div class="form-group">
<label for="id_username">Username: </label>
<input type="text" class="form-control" id="id_username" name="login" placeholder="Username">
</div>
<div class="form-group">
<label for="id_password">Password: </label>
<input type="password" class="form-control" id="id_password" name="password" placeholder="Password">
</div>
{{ form.username }}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}"/>
{% endif %}
<a class="button secondaryAction"
href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a>
<button class="primaryAction btn btn-block mt mb" type="submit">{% trans "Sign In" %}</button>
</form>
</div>
{% endblock %}
This is signup.html
{% extends "account/base.html" %}
{% load i18n %}
{% block head_title %}{% trans "Signup" %}{% endblock %}
{% block content %}
<h1 class="head-line">{% trans "Sign Up" %}</h1>
<p class="text-center">{% blocktrans %}Already have an account? Then please sign in.{% endblocktrans %}
</p>
<div class="container">
<form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}">
{% csrf_token %}
<div class="form-group">
<label for="id_username">Username: </label>
<input type="text" class="form-control" id="id_username" name="login" placeholder="Username">
</div>
<div class="form-group">
<label for="id_email">Email: </label>
<input type="text" class="form-control" id="id_username" name="email" placeholder="Emial">
</div>
<div class="form-group">
<label for="id_password1">Password: </label>
<input type="password" class="form-control" id="id_username" name="password1" placeholder="Password">
</div>
<div class="form-group">
<label for="id_password2">Password Again: </label>
<input type="password" class="form-control" id="id_username" name="password2" placeholder="Password Again">
</div>
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}"/>
{% endif %}
<button type="submit" class="btn btn-block mb">{% trans "Sign Up" %} »</button>
</form>
</div>
{% endblock %}
And this is logout.html
{% extends "account/base.html" %}
{% load i18n %}
{% block head_title %}{% trans "Sign Out" %}{% endblock %}
{% block content %}
<h1 class="head-line">{% trans "Sign Out" %}</h1>
<p class="text-center">{% trans 'Are you sure you want to sign out?' %}</p>
<div class="container">
<form method="post" action="{% url 'account_logout' %}">
{% csrf_token %}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}"/>
{% endif %}
<button type="submit" class="btn btn-block mb">{% trans 'Sign Out' %}</button>
</form>
</div>
{% endblock %}
View.py
if req.method == 'POST':
df = DocumentForm.objects.filter(document_id=id)
logging.info('LOG: I am here')
if df.exists():
for d in df:
description = req.POST['{}'.format(d.description, False)]
displayName = req.POST['{}'.format(d.displayName, False)]
df.update(displayName=displayName, description=description)
return redirect('/adboard/upd')
HTML File
<form class="needs-validation" action="{{id}}" method="POST" enctype="multipart/form-data" novalidate>
{% csrf_token %}
<div class="row mt-3 mb-3"></div>
{% if messages %}
<div class="alert alert-danger" role="alert">
{% for message in messages %}
{{ message }}
{% endfor %}
</div>
{% endif %}
{% for df in data %}
<div class="form-group">
<small class="text-muted bold-label m-lower">{{{df.field}}}</small>
<label for="validationCustom01">{{df.displayName}}</label>
<input type="text" class="form-control" id="validationCustom01" name="{{df.displayName}}" value="{{df.displayName}}" placeholder="Enter Display Name">
<label for="validationCustom01">{{df.description}}</label>
<input type="text" class="form-control" id="validationCustom01" name="{{df.description}}" value="{{df.description}}" placeholder="Enter description">
<div class="invalid-feedback">
Please enter required fileds.
</div>
<!-- <div class="valid-feedback">
Looks good!
</div> -->
</div>
{% endfor %}
<button type="submit" class="btn btn-primary btn-red btn-block">SAVE</button>
</form>
What I am trying to achieve is.
Pass a variable form to the user to fill
And I get the result.
I can't tell what the input id/name would be because it's a variable.
But I am finding it difficult getting the value from the view.py file.
change
req.POST['{}'.format(d.description, False)]
to
req.POST.get('{}'.format(d.description, False))
I am using Django Allauth for user flow in my project. I have successfully created my own set of templates which inherit from the standard Allauth templates and they work 95%. For some reason, however, I do not receive an error for an invalid login.
If I enter 'test#email' for the user I get a 'Please enter a valid email address' error but if I enter a proper email with an incorrect password it simply reloads the page but with no errors as to what went wrong. I've included my template below which has the {{ form.login.errors }} and {{ form.password.errors }} tags.
All guidance is appreciated, thank you.
Template:
<div id="search_container">
<div id="search_box_content">
{% block page_content %}
<h4>Login</h4>
<form class="" id="user-form" method="POST" action="{% url 'account_login' %}">
{% csrf_token %}
<h1> {{ form.login.errors }}</h1>
<h1> {{ form.password.errors }}</h1>
<div class="input_class">
<label for="login">Username: </label>
{{ form.login }}
</div>
<div class="input_class">
<label for="password">Password: </label>
{{ form.password }}
</div>
<div class="input_class" id="forgot_pass">
<label for="remember">
Remember Me? </label>
{{ form.remember }}
</div>
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<div id="modal_bottom_bar">
<a class="button secondaryAction" id="forgot_pass" href="{% url 'account_reset_password' %}">Forgot your Password?</a>
<button class="primaryAction" id="login_submit" type="submit">Login</button></div>
</form>
{% endblock %}
</div>
</div>
Some errors are non field errors such as those errors which are raised for some custom validation and are not included in field.errors rather they are in form.non_field_errors:
So better use this snippet to show all errors which belongs to fields and to custom validation if you are rendering your form manually:
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
I am trying to get the POST values of an .html page that has included pages via {% include %} in Django. However, it returns only the POST from the initial html page.
My inner html that is included has the snippet of code:
div id="edit_parameters">Edit Parameters</div>
<div data-id="{{job.slug}}" id="{{job.slug}}" class="collapse">
<form class = "form-inline" method="post">
{% csrf_token %}
{% for parameter in job.get_object.parameters %}
<p>
<input type="text" class="input-small" name="{{parameter}}" value="" id ="{{parameter}}-input" placeholder="{{parameter}}">
</p>
{% endfor %}
<button type="submit" class="btn btn-primary run-job">Submit</button>
</form>
</div>
{% else %}
<div>
No editable parameters for this job.
</div>
{% endif %}
And my outer HTML file has a snippet:
<ul id="available-jobs">
{% if jobs_same_io_type and jobs_diff_io_type %}<h3> Current Step </h3>
{% elif jobs_same_io_type %} <h3> Next Step </h3> {% endif %}
{% for job in jobs_same_io_type %}
{% include "includes/job_li.html" with add=1 %}
{% endfor %}
{% if jobs_diff_io_type %} <h3> Next Step </h3> {% endif %}
{% for job in jobs_diff_io_type %}
{% include "includes/job_li.html" with add=1 %}
{% endfor %}
{% if not jobs_same_io_type and not jobs_diff_io_type %}
<li class="full-height">There are no more available jobs.</li>
{% endif %}
</ul>
<fieldset class="submit">
{% csrf_token %}
<input type="hidden" name="job_to_run" value="" id="job-to-run" />
<input type="hidden" name="workflow_jobs" value="" id="ordered-jobs" />
<input type="hidden" name="job_to_edit" value="" id="job-to-edit" />
<input type="hidden" name="job_to_add" value="" id="job-to-add" />
<input type="hidden" name="job_to_remove" value="" id="job-to-remove" />
<input type="submit" name="done" value="I'm done, start the jobs" id="jobs-ready" />
</fieldset>
The everything that shows in post is in the inputs of the second snippet. But the parameters I want for the first snippet are not included when I use request.POST for all of the POST values.
Any help would be greatly appreciated in explaining why I am not getting the values from the included page and finding a possible solution. Thanks!
In HTML, only the fields in the form element that contain the submit button are included in the POST. the fields in your second file don't seem to be in any form at all, so they will never be posted.