I've already made lots of search but couldn't find the appropriate answer to my question.
Is it possible to submit form from template in admin panel? I have a template like this:
{% block content %}
{% load static %}
<form action="find_tr" method="POST">
{% csrf_token %}
Enter url: <input type="text" name="url"><br><br>
<input type="submit">
</form>
{% endblock %}
I can submit it from template itself but , what i want is to make it possible through admin panel.
For now it looks like this:
The same should be possible from admin panel:
Is there any way to achieve it?
Related
I am implementing a Formbuilder form in a wagtail website, but like to get the required value while looping over the form fields so I can show a required icon. The required seems to be in the query, but nothing I try shows me a result.
Create a loop through form
django doc
{% for field in form.visible_fields %}
<div>
<div class="form-group">
{{ field.errors }}
{{ field.label_tag }}
<input type="{{ field.field.widget.input_type }}" class="form-control"
name="{{ field.name }}" id="{{ field.id_for_label }}"
{% if field.field.required %}required="required"{% endif %}>
{% comment %}you might want form field for textarea or select etc. just make conditions for those {% endcomment %}
</div>
</div>
{% endfor %}
Wagtail form builder generates a normal Django form, so your best bet for custom styling is to explore how to style Django forms first.
I would recommend Django Crispy Forms as I have used it to customise how fields are presented and it worked well.
Their docs have a specific mention about how to override how required fields are rendered.
https://django-crispy-forms.readthedocs.io/en/latest/crispy_tag_forms.html#change-required-fields
Otherwise you could just use some custom CSS to put the icon where you need it for fields with the required attribute.
https://developer.mozilla.org/en-US/docs/Web/CSS/:required
I wanna include a page called group_sharer.html in another page:
{% block content %}
<script type="text/javascript" src='/static/media/js/group_sharer.js'></script>
<form action="." method="POST" id="my-form">
{% csrf_token %}
<select name="campaign" id="">
{% for campaign in campaigns %}
<option value="{{campaign.id}}">{{campaign.title}}</option>
{% endfor %}
<option value="test">test</option>
</select>
<input type="submit">
</form>
{% endblock content %}
Ive tried to include it to the home page with
{% include "group_sharer.html" %}
but its didn't delivered with its full data
What should i do know to implement the data at Group_sharer.html to the home page .
Your code is correct, the thing that is missing is campaigns,
Since you require these you also need to pass this context into the template thats including it.
From the docs:
An included template is rendered within the context of the template that includes it.
I've been avoiding using forms in Django for a while now because of this, and I feel like it is probably bad practice. When rendering forms in Django, everything is automated. This stops you from being able to conditionally render or hide certain parts of the form, attach different css classes to that form, etc.
What is the correct way to use Django forms in as dynamic a fashion as possible?
I'm in the same boat as you Jim, and I'm currently looking at https://docs.djangoproject.com/en/dev/ref/forms/widgets/. It seems a good starting point for customisation of fields.
As for conditionally showing/hiding elements, if you want this to be done on the form load (ie, a predetermined form layout, you could follow this SO answer which explains changes to the form's init: Conditionally show and hide a form field and set the field value
Or, if you mean how you could do this contextually depending on other selections as the form is being completed, the only way I've found up to now is to have some custom jQuery loaded in with the template that the form renders on. If there's a way to do this in-form, I want to know too :)
First of all, absolutely don't avoid forms. In fact, always use forms in django. They're simple to use and there's no reason not to.
Forms have enough flexibility to give you as much control as you need.
Typically you'll want to do something like this:
<form action="/myurl/" method="post">
{% csrf_token %}
{% for fld in form %}
{{ fld.label }} {{ fld }} {{ fld.help_text }} {{ fld.error }}
{% endfor %}
<input type="submit" value="Submit" />
</form>
You also might want to wrap fields in divs or a table. You might also test for fld.name to do something custom based on a field name.
Basing my answer on what Rainy has and referring to this and this, my answer would be
sudo pip install django-widget-tweaks
To enable widget_tweaks in your project you need to add it to INSTALLED_APPS in your projects settings.py file:
INSTALLED_APPS = [
...
'widget_tweaks',
...
]
I'd include this at the beginning of all the template files that I have forms to render.
{% load widget_tweaks %}
And render my forms as follows:
<form method="post" action="">
{% csrf_token %}
{% for fld in form %}
<div class="form-group">
{% for error in fld.errors %}<p class="help-block text-danger">{{ error }}</p>{% endfor %}
<label>{{ fld.label }}</label>
{% render_field fld class+="form-control" %}
<p class="help-block">{{ fld.help_text|safe }}</p>
</div>
{% endfor %}
<button type="submit" class="btn btn-success">{% trans 'Submit' %}</button>
</form>
I am using django allauth to my django project for all my authentication related functionality,
so now I want to implement password change functionality, so just copied the django allauth templates to my templates/allauth/account/password_change.html and customized with my custom design and has the form something like below
<form accept-charset="UTF-8" class="" action="{% url 'account_change_password' %}" method="post">
{% csrf_token %}
<div class="alert alert-success password_changed">
You have Successfully changed your Password!
</div>
{{form.as_p}}
<div class="span12 pagination-centered marg_lftnone">
<input id="save_new_password" name="action" type="submit" class="btn btn-large big_btn marg_tp38 marg_btm38" value="Save Password">
</div>
</form>
So with the above template the password changing functionality has been working fine and redirecting to current page, but what I want is when redirected to current page, I want to show a message div like above about informing that you have changed password successfully.
So how to display and error message after the password has been changed successfully and redirected to the same page?
Allauth emits password_changed signal, so you need to hook up a receiver. In your models.py add the following:
from allauth.account.signals import password_changed
from django.dispatch import receiver
from django.contrib import messages
#receiver(password_changed)
def password_change_callback(sender, request, user, **kwargs):
messages.success(request, 'You have Successfully changed your Password!.')
Then use your message inside template as documented here.
In django-allauth the messages are stored as txt files in django-allauth/allauth/templates/account/messages/.
You can copy the password_changed.txt file into your templates/account/messages/and customize the following code:
{% load i18n %}
{% blocktrans %}Password successfully changed.{% endblocktrans %}
Link to the password_changed.txt file on github
A nice simple way is to add this piece of code into the template password_change.html
<h1>{% translate "Change Password" %}</h1>
{% if messages %}
{% for message in messages %}
{% if message.tags == 'success' %}
{% translate 'Password successfully changed.' %}
{% endif %}
{% endfor %}
{% endif %}
<form method="POST" ...>
I'm creating a web app with django 1.2.4.
I am using contrib.auth.views.login, I have followed every step but it seems I have forgotten something cause I don't see the login form. Here is my folder structure:
/templates/
base.html
/myapp/
object_list.html
...
/registration/
login.html
...and here is my login.html:
{% extends "base.html" %}
{% block mylogin %}
<div class="horizontal">
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form action="{% url django.contrib.auth.views.login %}" method="post">
{% csrf_token %}
<div class="login_box">
<div class="login_text">{{ form.username.label_tag }}</div><div class="login_input">{{ form.username }}</div>
<div class="password_text">{{ form.password.label_tag }}</div><div class="password_input">{{ form.password }}</div>
<input id="button_login" type="submit" value="" />
</div>
</form>
</div>
{% endblock %}
...and in my base.html I have:
<div id="some_div">
{% block mylogin %} {% endblock %}
</div>
I have a basestyle.css included in base.html and the other templates inherit correctly too... it seems to be a block problem...
Any solution??
Thnak you
Instead of inserting of a block I used the include tag in base.html, just like this:
{% include "registration/login.html" %}
If you’d prefer not to call default (django provided) template registration/login.html, you can pass the template_name parameter via the extra arguments to the view in your URLconf.
For example, this URLconf line would use myapp/login.html instead:
(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'myapp/login.html'}),
Reference : Django official documentation
It solves my problem. Hope this works for others.