Possibility to fill in form OR upload file in Django - python

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 :)

Related

How to change browse button to look like modern drag&drop?

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 %}

How to save multiple sets of data from ul elements using one button in flask

I have some code that I am refactoring from using excel sheets to a mysql database within mysql.
Currently the app is set up like so;
There is a jinja template that loops through data obtained from a mysql table. There are multiple ul elements being displayed. Within the ul elements there is a checkbox and some other data.
What I want to achieve is to be able to go through 'checking' the checkbox if the data is relevant and then click a button to save changes.
However at the moment I only submit the data from the first row of the mysql table i.e. the first ul element.
I did try another way by attaching a button to each ul element and this worked the way I wanted it to, by posting the relevant data and was able to update the table accordingly, however my friend who I am refactoring the code for, wants to keep the universal 'save' button.
Is it possible to get all the results with one 'save' button?
Here is my html jinja code;
{% extends "home.html" %}
{% block content %}
<form action="/" method="post">
{% for x in data %}
<ul>
<li>{{ x.company }}</li>
<li>{{ x.name }}</li>
<li>{{ x.phone }}</li>
<li><input type="checkbox" name="relevant" value="Is relevant?" />
</ul>
<input type="hidden" name="company" value="{{ x.company }}" />
<input type="hidden" name="user" value="{{ x.name }}" />
<input type="hidden" name="phone" value="{{ x.phone }} }}" />
{% endfor %}
<input type="submit" name="save" value="Save Changes" />
</form>
{% endblock %}
Here is my app.py code (just the post part);
if request.method == 'POST':
if 'save' in request.form:
user = request.form['user']
phone = request.form['phone']
company = request.form['company']
if 'relevant' in request.form:
#mysql statement to go here to update table
#if check box ticked

Deleting an item from a list using a checkbox in Django

I have a file upload that allows the user to upload a file to the server and then they are displayed on a page in a list view.
Now I am trying to delete a file from the list using a checkbox and a delete button but I can't seem to get it to work.
I know there is an answer here on the same topic but it's not what I'm looking for. Django: writing a view to delete an item with checkboxes
here is the code that I have so far:
st.html
<!-- List of uploaded documents -->
{% if documents %}
<ul>
{% for document in documents %}
<li>{{ document.docfile.name }} <input type="checkbox" name="doc"></li>
{% endfor %}
<p><input type="submit" value="Delete file" name="delete"></p>
<button>Upload a File!</button>
</ul>
{% else %}
<p>No documents.</p>
{% endif %}
views.py
def stat1(request):
if request.POST.get('delete'):
Document.objects.filter(id_in=request.POST.getlist('doc')).delete()
I tried putting in a print() to see if the code after in the if in the views.py was executing and it's not.
I'm only new to python and Django so any help would be nice!

How to manipulate contents of a Django template based on value of radio button elements (without JS)

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.

Add custom admin add page/template in Django

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 %}

Categories

Resources