I'm new with Django and python. I have a problem. Is there a way, after click enter button to be redirected to the same change_form page without being passed to change_list page by Django? This is the UI that I mean:
(enter button in under all fieldset)
Hope you can understand what i mean and give me some responses. Thank you :).
Change the submit_line.html template
I've changed:
{% if show_save %}<input name="_save" type="submit" value="{% trans 'Save' %}" class="default" {{ onclick_attrib }}/>{% endif %}
to:
{% if show_save %}<input name="_continue" type="submit" value="{% trans 'Save' %}" class="default" {{ onclick_attrib }}/>{% endif %}
which will cause the default save-button to act as the save-and-continue-button.
this stackoverflow-question or the django documentation may help you to change the submit_line-template correctly
complete "submit_line.html":
{% load i18n %}
<div class="submit-row">
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_continue" {{ onclick_attrib }}/>{% endif %}
{% if show_delete_link %}<p class="deletelink-box">{% trans "Delete" %}</p>{% endif %}
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" {{ onclick_attrib }}/>{%endif%}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" {{ onclick_attrib }} />{% endif %}
</div>
If you are talking about the django admin, there's three buttons:
Save and Add New
Save and Continue Editing ( you need to click this one )
Save
If you're talking about your own apps, add a next parameter to your forms catch it in form validation and redirect accordingly
Edit:
I see you created a custom button, you can override the save/enter method in your ModelAdmin and redirect to wherever you want. Just add a hidden next input to your form.
Example:
settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
# other stuff
"django.core.context_processors.request",
)
your change form template:
<input type="hidden" name="next" value="{{request.path}}" />
the enter button, will cause the underlying html-form to be submitted.
this has nothing todo with django or python and is browser specific.
the django change_form has three submit-buttons, the last one will used.
there are multiple ways todo this:
override hange_form-template and add some jquery to react on the keypress event (enter) and fire the button of choice.
reorder those buttons
Related
I'm using Django 1.11
I was able to extend/override one template called change_form.html
I placed this file under /templates/admin/my_app/my_model/
For this one I extended and changed one block, like in the example in django docs
I was not able to override one template called submit_line.html
I tried placing it under /templates/admin/, /templates/admin/my_app/, and /templates/admin/my_app/my_model/. None worked.
I edited the file under django/contrib and it worked, but I don't want to change that file. It was just to see if the content was showing.
I just want to add one button to the template, so the user can download one XML file.
After some tests and research here is the solution.
First, underneath /templates/admin/my_app/my_model/ copy-paste the submit_line.html from django/contrib/admin/templates/admin/.
Change submit_line.html and add any urls you like. Say:
<!-- submit_line.html -->
{% load i18n admin_urls %}
<div class="submit-row">
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save" />{% endif %}
{% if show_delete_link %}
{% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %}
<p class="deletelink-box">{% trans "Delete" %}</p>
{% endif %}
<!-- NEW SUBMIT INPUTS -->
<input type="submit" value="TEST" name="_saveasnewss" />
<input type="submit" value="TEST 2" name="_saveasnews" />
<!-- END NEW SUBMIT INPUTS -->
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" />{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" />{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" />{% endif %}
</div>
We're copy-pasting it because there aren't any {% block %} tags inside the change_form.html to override.
Further on, inside the change_form.html, add these:
<!-- change_form.html -->
{% extends "admin/change_form.html" %}
{% load my_app_tags %} /* Change "my_app" to your app name that will contain the 'submit_row' template tag */
OTHER OVERRIDES HERE
{% block submit_buttons_bottom %}{% submit_row %}{% endblock %}
Finally, in your app's templatetags, add this template tag:
# my_app/templatetags/my_app_tags.py
from django.contrib.admin.templatetags.admin_modify import submit_row
from django.template.loader import get_template
from django import template
# this would be the path to your "submit_line.html"
t = get_template('admin/my_app/my_model/submit_line.html')
register = template.Library()
register.inclusion_tag(t, takes_context=True)(submit_row)
That's it! Now you should see these extra <input type="submit" /> only under my_model add/change page. If you want them globally just move submit_line.html from where it is to templates/admin/. Don't forget to update the paths inside my_app/templatetags/my_app_tags.py too.
So i just installed django-registration and got the templates from https://github.com/macdhuibh/django-registration-templates
I'm getting a problem with the URL resolver, I get
Reverse for 'auth_password_reset' with arguments '()' and keyword
arguments '{}' not found.
as well as many others....
urls.py:
#Other stuff,
url(r'^accounts/', include('registration.backends.hmac.urls')),
Exactly as the docs specify.
Furthermore here's the html that throws the error. It's from login.html from the github. It's the one that threw this error but it seems i get something similar every time i try doing a reverse match on a url from the auth_url.py of django-registration.
{% extends "main/header.html" %}
{% load i18n %}
{% block content %}
<form method="post" action=".">
{% csrf_token %}
{{ 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 %}
Thanks in advance.
Figured out what went wrong,
I added the URL pattern in the app's urls.py not the root project's one.
Hope this helps someone out too!
In a Django template, I have two radio buttons. If the second of these radio buttons is selected, I want certain follow-up elements to show. How do I do this in the Django template?
Here's my code:
<form method="post" action="" method="POST">
{% csrf_token %}
Privacy:<br>
{% for radio in form.private %}
<table><tr><label for="{{ radio.id_for_label }}">
<td><span>{{ radio.choice_label }}</span>
<span class="radio">{{ radio.tag }}</span></td>
</label></tr></table>
{% endfor %}<br>
{% if radio.tag == 'value1' %}
{{ form.rules.errors }}
{{ form.rules.label_tag }}{{ form.rules }}<br>
{% endif %}
<br>
<input class="button" type="submit" value="OK">
</form>
Currently, I'm trying {% if radio.tag == 'value1' %} to try and accomplish what I posted in my question at the top, i.e., if the radio tag has a certain value, element called rules will display. Needless to say, it's not working for me. Any suggestions? I'd prefer not to use Javascript, because more than 80% of the users using this system have legacy phone devices that do not support JS.
Note: please ask me for any clarifications, where warranted. I may not have successfully provided all the information you need.
You need to compare as follows:
{% if radio.choice_value == 'value1' %}
radio.tag is a method that returns the entire HTML of the input, which is why your comparison fails.
I'm currently using django-allauth to manage my registrations and logins.
Until now, all of my logins and signups have worked by having the django-allauth pages at the /accounts/ prefix for url patterns, so to register a new account you would navigate to /accounts/signup/. I want to keep this functionality, but also want to introduce a signup form onto my landing page (at my base url), and I want to have a quick login form in my top banner from anywhere in my website (not /accounts/).
My question is simple, how can we create login and signup forms anywhere throughout the site, without restricting it to a specific prefix of my urls?
If you want to create a signup/login form in your landing page or somewhere else, you can take a look at this question: https://stackoverflow.com/a/24179796/2230003
Basically, for a login/logout form using e-mail only and not username to login, the code in your template would be:
{% load account %}
<h1>Login / Logout</h1>
{% if user.is_authenticated %}
<p>Loged in with e-mail: {{ request.user.email }}</p>
Logout
{% else %}
<form action="{% url "account_login" %}" method="post">
{% csrf_token %}
<input type="email" placeholder="E-mail" name="login">
<input type="password" placeholder="Password" name="password">
<label for="id_remember_menu" class="text-primary">Remember Me:</label>
<input id="id_remember_menu" name="remember" type="checkbox">
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<button type="submit">Login</button>
Forgot Password?
</form>
{% endif %}
How do you load both the Social Login, the Login form and Sign Up form to your Index page using django-allauth?
Something similar like when you go to facebook.com.
accounts/url are already working and I've tried copying the
<form class="login" method="POST" action="{% url 'account_login' %}">
{% csrf_token %}
{{ form.as_p }}
{% 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" type="submit">{% trans "Sign In" %}</button>
</form>
to my index.html but it doesn't work.
I'm new to Python Programming and Django development but i've done some excerise with tutorials in the Django Book.
The view that handles your 'index' url and renders your index.html template has to have a login form, for that you have to make it a FormView descendant, and set the class attribute to LoginForm (allauth's login form). Otherwise, you can't render {{form.as_p}} to the template (because there is no form in your context).