I am trying to create a model via a form that has multiple other models related to it. Say I have a model Publisher, then another model Article with a foreign key to Publisher. When creating a Publisher via a form, I want to create An article at the same time. I know how to do this via formsets. However, I don't know how to add a button that says add extra article at the same view, without having to be redirected to a new page and losing the old data since the form was not saved. What I want is when someone clicks add new article, for a new form for article to appear and for the user to add a new Article. Is this possible to be done in the same view in django, if so can someone give me and idea how to approach this?
I would show code or my attempts, but I am not sure how to even approach it.
This can only be done using JavaScript. The hard part is to have the management form sync up with the number of rows.
But there's two alternatives:
Semi-javascript (Mezzanine's approach): Generate a ton of rows in the formset and only show one empty. Upon the click of the "add another row" button, unhide the next one. This makes it easier to handle the management form as the unfilled extra's don't need any work.
No fix needed: Add as many rows as is humanly sane. In general, people don't need 40 rows, they get bored with filling out the form or worry that all that work is lost when the browser crashes.
Hope this helps you along. Good luck!
Related
i have a doubt, i want to save a form and without use a submit button.
Because, as you´ll see:
i need to increase "cantidad" but i don't want to use a "sumbmit" button, otherwise, i need that the data (integers) will stay correctly in a database field.
Because, in my models.py i have:
cantidad = models.IntegerField(default=0)
I want to modify the field 'cantidad' when the user increase or decrease Selection, as you see in the image and without a submit button.
Is there a way to do this?
Any help thank you!.
After talking to you, something like this will work, but you'll have to modify it to your needs. I would use sessions because their values are stored without having to save your current modelfield in the database, and users could still browse the site without having to update the values every time.
Remember: Sessions are accessible like dictionaries. But you have to .save() them just like you would any model, because it's still a model stored in the database. The reason you should use these instead of your product model is because sessions are intended to be random, and are easy to clear with python manage.py clearsessions. If you used your own model, assuming you have thousands of models to dig through which were never purchased by someone, this is FAR EASIER and faster, and keeps your database cleaner.
This is a quick answer, though I suggest you implement a shopping cart that's already been made by someone else. Check djangopackages.org for some good ones. What they have will be better than what I'm about to show you, though this is the principle you follow either way.
GENERAL STEPS TO TAKE:
Make a session key: request.session['cart'] = {}
Add keys and values to it: request.session['cart']['product_id'] = 111
Add more values to that product: request.session['cart']['quantity'] = 222
Save it: request.session.save()
I've been able to easily create a list of editable items and manage them through the Django admin panel. That seems pretty simple and I have a good idea of how models work from other frameworks.
Although, I'm curious to know to know how I can make something as simple as editing a text area on a static site. Basically, I don't need to "add post" or anything of the sort. I just want to be able to edit a text area on a static site.
Any docs or examples?
I've been looking at packages such as django-flatblocks and chunks, these seem to possibly help with what I'd like to do. I'm just new to the Python world so some of this stuff is a little magic to me, especially involving the administration panel.
One thing that I realized is that these libraries (or apps in django?) seem to be super out of date, stemming back to the last commit being from 2012. Are these still commonly used?
Figured out a solution in case anyone is interested. I ended up using a Django application called django-generic-flatblocks which seems to provide me with what I need. Although, it's a bit strange as I had to replace all the text on my site with a gblock and then re-enter it all. Seems as if upon first creation of a block, it's empty so you essentially have to provide it with a value.
After that, if you log into the admin panel you're able to pull up the block and edit it. Alternatively, if you're logged into the Admin panel you're able to view your site and an edit tag is provided and you'll go directly to that block in the admin panel.
If anyone knows of anything which essentially allows me to define the text blocks up front in the admin panel then add the tags to the code, please lmk. I'd prefer to load all my content into the admin panel first and then just throw the tag into the code and have it display. That would save a lot of time in terms of having to copy the existing content, store it away, adding the tag, and then having to put it back in.
This seemed to be the only one of the recommended apps that worked for me. I tried to use Chunks because this really is only for title/text but on Django 1.11 it would freak out on me about not having South.db, which isn't even used.
https://github.com/bartTC/django-generic-flatblocks
This is really frustrating,
I can't and can't find how to create a form (I'm guessing a forms.Form form) to update just one field of a more complex model.
The model has 5 fields, and a form to create, update all of them.
But in a different case i need to let the user update only the title (a field in the model), so i need tried so many things until now (including creating an HTML form by hand and from the view to save it, creating a forms.Form and many more, nothing seem to work), There is no code here because i don't even know which one to put....
Maybe some one can help me with that, I'm sure it is a simple thing, But for some reason i am stuck on this for a long time...
Thank you,
Erez
If you're using ModelForms, you just have to define a fields attribute in Meta as a tuple containing just the names of the fields you want. See the documentation.
[Preamble: Whereas I realize there may be simpler ways to do this (i.e., just use Django built-in Admin, or use inlines to do all editing on one page, etc.), unfortunately, I am not in control of the designs, so I'm seeking help on how to work with what I've been tasked with implementing.]
I have 2 models, Deck and Slide. Slide has foreign key to Deck. (there is also an intermediate model--its a m2m relationship--but to simplify an already complex discussion, I'm going to pretend its a simpler many-to-one relationship.)
The interface i am implementing displays a page with a form to enter or edit Deck fields which also includes an embedded list of Slides in the Deck, with some fields (which I'm in the process of making text inputs) and also with an "edit" and a "remove" anchor link for each Slide. (see img) If you click an "edit" link, it takes you to a new page with a detailed form to input all the information representing the content for the corresponding slide. If you click submit in that form, it takes you back to the page for the deck.
As the title question proposes, I obviously don't want to commit any Deck or Slides to the DB until a user clicks Submit for the entire Deck, i.e. they can add or edit many slides in the interim and may decide to cancel the whole process.
What is the best, cleanest way of doing this?
I've looked at Django's FormWizard class ( http://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/ ), but seems to be geared towards a multi-step linear flow, not my situation.
I already had to implement this for a demo, and I've gotten most of the way there by creating an inline FormSet for the Slides and a Form for the Deck, and then writing subclasses for my Forms that hide the form and the formset as I pass back and forth between the two pages. Here's some code demonstrating how I use a hidden form for the deck in the slide page:
class DeckForm(ModelForm):
class Meta:
#stuff here
class HiddenDeckForm(DeckForm):
def __init__(self, *args, **kwargs):
super(DeckHiddenForm, self).__init__(*args, **kwargs)
for name, field in self.fields.iteritems():
field.widget = field.hidden_widget()
field.required = False
So in my views, every time I go to the slide form page, I generate a HiddenDeckForm from the POST data and pass it in, and then in my view going back to the deck page, I regenerate the DeckForm (not hidden subclass) from the POST data. Not going to post all my Slide form code, since I'm basically asking whether there is a better way of doing it, but similarly, I have a HiddenSlideForm class, and pass a formset of those between my templates to save the state.
Then when the user clicks submit on the Deck page, the Deck form and Slides formset are all saved to the DB.
It works, but is this a good way to do it? Its been a good bit of code, and I've really had to dig into some Django internals--feel's like I'm using things in a way they weren't designed to be used. Or is there already a more standard way of handling this scenario?
I'll post more code if its helpful.
Thanks for the help!
PS As you can see, I'm implementing it in a jquery colorbox and using ajax for the transitions between the forms, but I am just using normal template/form rendering in my views and passing the rendered html back to the page with an ajax call . . . guess I could do something with json, but strongly prefer to stick with passing a rendered template, as such an implementation could be used without ajax or javascript if desired.
Idea A: Store the uncommitted data in the session until everything is ready for commit. Each form/roundtrip to the client just adds/updates the accumulating data. If they abandon it you only have crap lying around until the session is destroyed. You can save pretty much anything to the session.
Idea B: Add a boolean to the database table which indicates when a row has been committed (eg: "is_active" or "is_pending"). Means you never lose anything but a bit more of a nuisance to manage.
Having done the very similar requirement by working around the formset tools, I completely agree that going hoops and bounds to understand/customize the formset is totally not worth it.
For a screenshot like yours, I'd use just one form with all of the slides per deck. - Form, not formset.
You should handle the slide "edit/delete/new" all in ajax requests that include the deck you create when someone creates a "new deck". And then in the "Deck Form", you only change the Deck properties like name and association.
Or, if you are inlined to do all new elements in the page itself without Ajax and creating new "Slide" objects, you can use the formset and save the deck and all associated slides.
I need to be able to create forms from admin panel. Process would look like this:
I click on "Add form" then I enter email to which the form should be sent and of course several fields (probably thanks to inlines) consisting of field name, type and if it is required. User should be able to view and fill the form and submit it and the data should be sent to the email given in admin.
Everything looks pretty straightforward but from my point of view it need some metaclass programming skills.
Could anyone point me to a goot form builder for Django or at least hand some tips about creating such thing? I found django-forms-builder but it is a bit too restricted imho.
I know this one's a few months old but I just though I'd post an update here anyway for anyone else that comes along.
django-forms-builder has just been rewritten to do exactly what you were looking for when you originally posted this question.
You can find the new version at http://github.com/stephenmcd/django-forms-builder or http://bitbucket.org/stephenmcd/django-forms-builder
There are many alternatives, although not many of them are actively maintained:
https://www.djangopackages.com/grids/g/form-builder/
If you want to have a full control of what's happening (change fields for your needs or add new ones, add captcha or honeypot, add custom handling of form data, use form wizards or even use your forms via web REST API), use django-fobi https://pypi.python.org/pypi/django-fobi