create an object using the form django - django

Thank you for looking into this!
I doing one of those django tutorials from their official website, creating a poll. The poll app and everything is working.
The problem is that after I have created authentication for the users to log in they should be able to create the polls, aka they're given the fields with questions/choices, fil it in and from that data(form) it should a new poll object into the db. I have set up everything, but I cannot figure out how do I write the view for this, as in how do I extract data from the form and add it all as a new poll.
I am using three models, as in tutorial: polls, choices and user (user isn't recognisable either, i mean in the model 'user' i have a variable name = models.ForeignKey(User), I was using django-registration to register the them, but that's not the main problem at the moment).
I hope I am more or less clear, if not, I will be glad to explain again:)
thanks, blargie-bla

I'd recommend you starting with generic views.
https://docs.djangoproject.com/en/1.3/topics/class-based-views/#decorating-class-based-views
https://docs.djangoproject.com/en/1.3/ref/class-based-views/#editing-views

Related

Django: Last modified by and created by user automatic saving

The age-old question: How can I automatically save last_modifed_user in django models?
I found in several places this general process how to do it using thread local. I'm hesitant to simply implement it that way because I'm not entirely sure of the consequences it has and because all these posts are old.
Is using thread local still the "recommended" way of doing this in django 3? Or does django3 have a better options of doing it?
No, this hasn't changed. Simply because separation of concern is an architectural principle of MVC (model-view-controller), which is also how Django (model-view-template) and most web frameworks with ORM are architected. Models know nothing about the request, it's not available (and in many cases there isn't a request at all when a model is saved, think of management commands or regular tasks running in the background).
The alternative to thread local is to make sure you implement it yourself in the controller layer (view layer in Django):
Create a view mixin that you can mix with all the generic views that use the ModelFormMixin to save the user into the model (ModelFormMixin.form_valid()). Or combine it with a form mixin where the user is passed to the form (FormMixin.get_form_kwargs()) and saved when the form is saved (ModelForm.save()).
Create a ModelAdmin mixin that does the same when saving a model in the django admin site.
This of course means someone on your team may forget to do it when creating new views and forms. The link you posted contains an answer as to the advantages and disadvantages of using thread local.

How to Fill Form with Data in DJango without Update

New to Django here. I have a link to a form in DJango. I use the CreateView to have the user enter the initial information. It all works great and the data is accurately saved to the database. My issue is this: I would like that same link to open the form (it's a one-to-one relationship) with the filled data so the user can see what they have previously entered and correct, edit or update as needed. The form currfently opens as a blank form so if the user has entered that information previously they are unable to see it. I cave researched get_or_create and update_or_create as well as a number of other topics, but can't seem to figure this out. This needs to be a user-friendly experience so multiple entires or clicking multiple buttons to access the data is not an option. How best can I implement this?
#Don you can checkout django formsets, I think this will help in this situation. And you can use a single FormView for all your needs by overriding its methods.
Have you looked at Django Sessions. It’s a simple way of saving session data and passing the data to future requests. https://docs.djangoproject.com/en/3.0/topics/http/sessions/. I. In your form view you cloud save the session data you want to pass to your next form. In your next form, you could use the session data as default values. I’ve done something similar in the past.

Creating a Django Rest Api to Store Values

I have a python parser who creates two python lists, first one contains some IDs and the second one contains ip:port information which corresponds to those ID's. I need to create a django rest api to send these values and store them. I tried to read django documentation but I still don't know what to do, where to start. Can anyone help me? Thanks in advance.
The question is not easy to answer, although I can show you the right direction.
Django uses the Model object to interact with database. Therefore you should create one model with two fields. The first one is ID (although read the documentation carefully, Django can create the ID automatically for you) and the second one should be a String (or really object of your choice) to store IP/Port information. For information about models, visit the following page:
https://docs.djangoproject.com/en/2.1/topics/db/models/
To submit your information to Django back-end, POST request should be used. What is POST request is beyond the scope of this comment, so I will just expect that you know what it is. Open your urls.py file and make an endpoint for this particular request. More information can be found here: https://docs.djangoproject.com/en/2.1/topics/http/urls/.
To create a form for user, Django offers great object called Form. You need to create one in the forms.py file. To give you an example:
from yourapp.models import YourModelObject
class InformationForm(forms.ModelForm):
class Meta:
model = YourModelObject
fields = ('id', 'ipportField')
To read more about forms, check the following doc: https://docs.djangoproject.com/en/2.1/topics/forms/

How to create Models in Django that act like a CMS?

I need help developing models in Django's ORM that would give me the ability to create a user profile and then create unique pages under that user. What I am thinking so far is that I have one model called users and then another model pages. When the user is created and that user creates a page the data in both models are linked by a unique id. Is this the right direction to go or is there a more standard way of doing something like this? Essentially, what I am trying to create is a really stripped down version of Wordpress if that helps.
Any advice would be greatly appreciated.
Thank you

How to save frontend user login/registeration intomation in django project

Am new to django. Any one could suggest me, How to handle frontend user in django project. Such as how and where to save frontend information like first name, last name, email id. Should i create new table in db or save this infomation in admin user table.
Sorry for me bad english.
Thanks.
The base user model can handle this. Docs here. It also comes with some useful built-in forms to help users register - docs here.