Django - pass data from basic HTML form into model - python

I have created a simple app in Django using some tutorials, but it became very usefull and also scaled a lot.
What i have is a basic HTML table - prefilled with data from Model A.
At the end of the table there is Submit button, which just use some javascript to prompt a print window (to save the table[page] as PDF basically)
What i would like to do, is that when i press the Button to print, i would also pass some data from the table for example ModelA name and adress into a ModelB - which would serve as an statistic.
However i have used for this a simple tutorial, and therefore to display the table i used "DetailView" in views.py. This is my views.py file
class PrinterDetailView(DetailView):
model = Printer
template_name = 'printer_detail.html'
Is it possible to achieve this without redoing the whole site? i Found some people searching for simillar answers, but from that it seemed like i would have to redone whole app..
Thanks for your input!

Related

Creating a templete with xlsxwriter in django webframework

I'm new to django and still trying to figure out logic. I watched a bunch of videos and I know how to create those examples but I'm not quite there. I'll explain what I want and maybe someone can explain to me a logic how to do, yes?
I created an app and inside an app I connected urls, settings, html and I have working site. Now I want to input data in django website, django to pass data to xlsxwriter script to create xlsx document, and to pass that document to user to download.
My logic: What I need now, is to create some kind forms (but not for registered users, it can use anyone who uses a website) to input data. Which forms are best? Than I need to pass () that data to function that are in views that will get an arguments from html page through some kind of forms, create document, and pass that to download button.

share button for own site Django

I want to implement a ''share on my profile'' button on my page but I have a hard time how to do so. When I try to find something like that (google/here) the results are "how to share something from your site on twitter/facebook". If there is a similar question please share the link I was not able to find something.
So i have a site where users have a profile where the liked content is displayed and I would like to give the user the option to share a Post on his own profile with a personal comment(like FB/twitter does).
My problem is that I don't know how to implement this into my models. If I want to save the "shared" post in a QuerySet in the UserProfile model I don't know where to save the comment for each shared post. If I make an extra table for all the shared posts with a extra form combined its a total mess since each post is saved individually and I don't know how to combine the existing image in to the form where the user writes his comment .
Can anybody tell me in which direction I have to walk? Feeling a bit lost on this one.
You don't need to make an extra table, django will do it for you, if you'll use ManyToManyField in your Post model.
See this docs https://docs.djangoproject.com/en/1.10/ref/models/fields/#manytomanyfield

How to do a user image upload - Django

I really need a lot of help with this and I dont know where to start since I am new to Django so excuse my ignorance.
I'll explain. I am building kind of a image uploading-based page. The images are grouped into albums. I created the album model and the image model which belongs to an album and it has its foreign key. But I dont know how to do the upload itself. I thought I just had to handle the data taken from the template in the views.py file and kind of upload it; but I am getting confused with some things I saw about a form... I dont know. I have no idea.
So its simple: How do I upload the image and register a new row in the database, submitting all the additional data (like pic description) the module requires? Please help with this.
Looks like you are confuse about django forms. If you define the forms.py and simply do {{ form }} will render all the field in html so you don't need to worry about defining in html form field. or Don't use form... Define all the field by yourself (!mportant Client side validation is must )
Write the views like save the file into Image Table that's it. If you need anyhelp here . Happy to assist you ..

Django: how to handle data that doesn't fit in a model

I have been working with Django for a while now and I think I'm getting the hang of it. Working with database data is pretty easy with models. However, I am really struggling to find a way to handle data for which making a model isn't really an option. For example, I want to have a "welcoming" text on my homepage that should be editable from the admin interface. Other examples are an email address for contact information, a path to a file (eg resume) stored on the server, links to social media, ...
Making a model for this kind of data seems unnecessary to me, as there will only be one entry in the table for that model. Up until now I have been using Constance for a while, which is pretty nice. However, I'm interested to find out how I should handle this kind of data with "vanilla" Django
Why not have a model for bits of text to be displayed on your site. It could simply have a unique name, and a text field.
from django.db import models:
class SiteText(models.Model):
name = models.CharField(max_length=64, unique=True)
text = models.TextField
Then it's easy to create a simple bit of text in the admin panel, or in code:
SiteText.objects.create(name="greeting", text="Welcome to my site!")
And accessing that from within your site is as simple as:
SiteText.objects.get(name="greeting")
It's fine if this model has only one instance, but it also allows you to store other snippets of text in the database and easily access them from other parts of your codebase.
For your other examples, paths to resumes, email addresses, links, these all sound like they could be fields on a user model, or separate tables with foreign keys to a user. Storing things like this in the database is perfectly reasonable.

Django modelform: Create new related object

I've been searching stack overflow and google for a solution for over an hour now, and I can't seem to find something that, in my opinion, should be easy to obtain (as it's a common use case).
I've checked this thread, and a few others, but I haven't been able to find a real, easy solution:
Django modelform: is inline adding related model possible?
Anyway, say I have a model with three related entities, two foreign keys and a many-to-many related class. Now, I have a ModelForm which displays these in comboboxes and lists, but what I need is that "+" button next to these elements (as seen in the admin interface).
I want the plus to take me to a new form, for that particular entity, allow me to submit the new information, create the database entry, take me back to my original form and have the newly added entity selected in the combobox. I'm really hoping the django ModelForm Meta class has an attribute that I can't seem to find which enables exactly this.
This isn't really a django question.
This has to do with presentation of a particular widget in an html document, and that is governed by either the HTML markup, CSS, or javascript.
Django is a server side application and is primarily responsible for creating a valid http response and receiving a valid http request (of course, there is a lot that happens in the interim and that is why django is so big) but it's not a "one toolkit to kill them all" app.
I think you want to look at bootstrap: http://getbootstrap.com/
Jquery UI: http://jqueryui.com/
Or some combination of the two.
You can also just mark up the document yourself with a stock img or something.
However, if you want to do it exactly how the admin does it, just go into django.contrib.admin and examin the code to figure out how the django developers did it. I believe they are just using Jquery UI and some manual markup to accomplish that.

Categories

Resources