In my current Django project I need to implement one admin page that loads some asynchronous data with AngularJS to fill the form fields. Some form fields (on client side) will be updated by AngularJS.
My question is: What's the right approach to add this complex add/change page of my AdminModel? Should I create a pure json / AngularJS client side page?
The forms fields will be the same of my Model field, so I would like to use ModelForm.
I tried with:
class ContractAdmin(admin.ModelAdmin):
add_form_template = "admin/add_contract.html"
and then in my contract.html template:
{% extends "admin/base_site.html" %}
{% load i18n admin_urls admin_static admin_modify %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }} <!-- this produces nothing -->
<br />
<br />
<input type="submit" value="Submit" />
</form>
{% endblock %}
Related
I have created a django site, django site is working in this way. You go to the site, upload document with data and you get some results. You upload excel file, and you download excel file. In background I used pandas for calculations. In the end you can see code.
I have left one thing to finish. I want to create drag&drop for upload document. Chech picture 1, as I understand this is already drag&drop, just doesn't look as I would like to. I want to look like in picture 2.
Picture 1: Current
Picture 2: I would like to look like this.
Can I change the look that you seen on first image without using js?
This is html that I have and output is shown in picture 1...
{% extends "base.html" %}
{% load static %}
{% block content %}
<form action="{% url "cal" %}" method="post" enctype="multipart/form-data" class="dropzone">
{% csrf_token %}
{{ message }}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
</p>
<p><input type="submit" value="Upload and Download!"/></p>
</form>
{% endblock content %}
This is function in views
def OnlyCAL(request):
if request.method == "POST":
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
output = io.BytesIO()
newdoc = request.FILES['docfile']
#pandas calculations
response = HttpResponse(
output, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response
else:
form = DocumentForm()
return render(request, 'cal.html', { 'form': form, 'page_title':page_title })
I think the best option for you is to use dropzone.js. You can find documentation here.
You should include .js and .css. Also you can customize dropzone view on your own as described here (example of customizing dropzone in combination with Django, maybe will be useful for you)
You can try this (.js and .css files included directly in your template just for example, you can include easily include it in other way):
{% extends "base.html" %}
{% load static %}
<script src="https://unpkg.com/dropzone#5/dist/min/dropzone.min.js"</script>
<link rel="stylesheet" href="https://unpkg.com/dropzone#5/dist/min/dropzone.min.css" type="text/css" />
{% block content %}
<form id="dropZoneForm" action="{% url "cal" %}" method="post" enctype="multipart/form-data" class="dropzone needsclick dz-clickable">
{% csrf_token %}
<input type="hidden" name="abc" value="i am hidden value">
<div class="fallback">
<input name="file" type="file"/>
</div>
<input type="submit" id="submit-all" value="Upload and Download" class="custom-button" style="float: right">
</form>
{% endblock content %}
I am currently trying to make a platform for Formula One statistics. Which of course should be able to add circuits (my first form). And I let Django make the form so that works, but I would like the option to fill in manually or upload a file so he will parse the code.
Here is my form:
class CircuitForm(forms.ModelForm):
class Meta:
model = Circuit
exclude = []
Here is the page that is displayed:
{% extends "base.html" %}
{% block content %}
<h1>Welcome in the uploads</h1>
<h2>You can add a single circuit here</h2>
<form method="post" enctype="multipart/form-data">
<table>
{{ form }}
</table>
<br><br>
<h3>Or you can upload a file for bulk-uploading:</h3>
<input type="file" name="circuitsFile" value="Upload a file">
<br><br><br><br>
<button class="button" type="submit">Submit</button>
</form>
{% endblock %}
When you press the submit button, it doesn't do anything since the other fields need to be filled in first
If anyone knows the solution, I'd like to hear it :)
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.
I have been stuck on this for a while. I am trying to put both the login and registration forms on the same page, however I have been getting multiple errors depending on which approach I try. The page is actually able to load fine, however neither of the form fields will load. And then when I hit the register or login button (both type submit), I most recently am getting a csrf related error. Any suggestions on how to implement both forms on the same page would be greatly appreciated.
I think it might be csrf token not found error.If you are using django 1.3 with csrf middleware you have to put csrf token {% csrf_token %} tag with each form posting data like this:
{% extends "admin/base.html" %}
{% load i18n %}
{% block content %}
<form method="post" action=".">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" />
</form>
{% endblock %}
I'm using django 1.2.4.
I have a template for login in registration/login.html (wich action is django.contrib.auth.views.login) and I want to include it on everypage. I created a block on my base.html as I do for every template. The thing is the browser doesn't recognize this login block and I think it is because I only render a template for each view, I am not rendering this login template.
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...
So.. how can I render this template for every view??
Thank you
I think you are looking for the include tag. This way you can include your login html snippet in your base.html :
{% include "/registration/login.html" %}
What I really need was to create a templatetag in templatetags/mytags.py, where I define a function called get_login wich looks like this:
#register.inclusion_tag('registration/login.html', takes_context=True)
def get_login(context):
...
return {'formLogin': mark_safe(AuthenticationForm())}
...and in base.html:
{% load mytags %}{% get_login %}
The problem now is that the template (registration/login.html) doesnt recognize '{{ formLogin.username }}','{{ formLogin.password }}' and so on.
What am I missing?
Update 1:
mark_safe returns an instance of django.utils.safestring.SafeString, not a form.
Use (AuthenticationForm() instead of mark_safe(AuthenticationForm()) and it works!