How to do a user image upload - Django - python

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 ..

Related

Django - pass data from basic HTML form into model

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!

Is it bad to design form layout in forms.py Django?

I'm new to Django. After few weeks learning, I've designed my form layout inside forms.py, like this:
But it's quite annoying when I have to write html tag in string format without IDE styling, So do you guys have any ideas to design form layout in Django that is more clear and easy to maintain? I'm using crispy_forms btw.
Thank you...
It's not bad as there are no ways to tell whether practices are good or bad as they're mostly up to personal flavor. My take, however, is that crispy forms do not provide enough flexibility. Plus, once you moved on to complex form controls, you need multiple divisions and the hierarchy will be stacked.
It's up to you whether you want to keep this, but I'll tell you how I get past the forms.
I render the form, no styling, and anything. I then inspect the page and then note down the id, name, field type (autocomplete, required, etc.) of every input as Django needs those to tell your data apart from others.
I don't directly load the form like {{ form.as_p }}. I include the action, URL, and then apply my custom form but use the id, name, and everything else that the default form uses.
So you'll have the same Django field attributes but they are applied to your custom HTML form. It's definitely more work, but from my experience, this pays off.

Django; What is preferable way to upload some images?

I've been working on Django project. Currently, user can upload only one image but now I want to change it so user can upload some images.
models.py is like this.
class Entry(models.Model):
photo = models.ImageField(...)
I am thinking of just adding photo2 and photo3 inside Entry model. But I'm wondering if there's a better way to do it. I don't want to delete images that are already uploaded. Anyone who could give me tips? Also, I don't like the file upload form's design and some people use just button-like form. I also want to know how to create button-like form.
Create new model called 'Photo'.
Create relationship from 'Entry' to 'Photo'.
If you want to use photos that related with Entry record, you need to use select_related() function
Also you can find button-like upload form here.

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

mongoengine - Do i need to create form for a model ? How to validate ListField(ImageField()) either by form or directly

i am using mongodb for django using mongoengine .
Usually for normal models, we write forms and the after submisson of the input data(html form), we populate the django form which validates the data . If form is valid, we just save the data in django form to the model . Do I need to follow the same process for mongoengine also ? If yes, how can i validate an image field. My image field is like this :
images = ListField(ImageField())
Also, how should I ask the user to enter a list of images in the html page that can be saved into the above field . Will provide more information if necessary
Thanks.
When adding a new image to the ListField if you want to validate you can call Model.validate() to ensure its valid.
ImageFields take the actual image file - you can see some examples of their usage here:
https://github.com/MongoEngine/mongoengine/blob/master/tests/test_fields.py#L1880-1968
One caveat - I havent tried an ImageField within a ListField so let me know how it goes :)

Categories

Resources