I can't really seem to find the answer anywhere.
Basically, I have data in my view coming from an API that I want to pass into form fields. Is that possible?
Related
I have a model that will be displayed in a ListView. One of the fields in the model is category which has 3 choices. When I display the template, I want to be able to tell between the 3 categories. I considered overriding get_context_data() to add a context for each category by getting all the objects and filtering them.
Would that be a better approach than a custom template tag?
And even if it is, assuming I still want to create the custom template tag to accomplish this, how would I write it? I know how to write custom tags, but I am unsure how to write one such as this.
Only idea I can come up with is to create a tag something like:
#register.simple_tag(name="is_cat1", takes_context=True)
def is_cat1(self, context):
objs = context['object_list']
if (MyModel.objects.filter(category__icontains="Cat1") in objs):
objs = MyModel.objects.filter(category__icontains="Cat1")
return objs
Could anyone provide an example of such a filter that deals with models like this? As well as answer my question as to whether it would be better to use context?
Thanks
Using AJAX is the correct solution. Such an operation doesn't fit with Django's framework and intended use.
I'm currently learning django from the 'How to tango with django' site and i'm unable to understand the chapter dealing with forms.
Appreciate it if someone would help me out.
http://www.tangowithdjango.com/book17/chapters/forms.html
the first step is to create a forms page which maps to models.py. I seem to understand this part. I also understand that we create
a view to process the data acquired from these forms. I'm not able to understand the below code in the views page.
from rango.forms import CategoryForm
def add_category(request):
# A HTTP POST?
if request.method == 'POST':
form = CategoryForm(request.POST)
else:
# If the request was not a POST, display the form to enter details.
form = CategoryForm()
How do the urlmapper know that a request method is POST or GET before the user actually enters any data in the form?
On a similar note, when would a form have a get method?
form = CategoryForm(request.POST) - would someone explain this to me? CategoryForm looks to be a class which is already inheriting from another class
what does the request.POST argument convey ?
1) The urlmapper does by default not care about GET or POST request method. It will route any request to the given view-function.
Normally, your form html-code will look like this:
<form method="post" action="some_url">
...
</form>
So, when you submit the form, the data will be send to some_url with the specified method, in this case post.
You may want to read something about when to use GET or POST, normally forms are transferred using POST.
2) form = CategoryForm(request.POST) will bind the values provided in the request's POST-dictionary to the form. You may say, it prepopulates this. This way, further working with the form (like validating it by calling form.is_valid()) will be made possible.
Perhaps you should investigate further on Django forms and modelforms by reading some official documentation.
Why do you think the URL mapper knows if it's a post it a get? It doesn't, and it doesn't care.
The thing you are missing is that this view has two responsibilities: showing the initial form (on GET) and processing the submitted form (on POST).
Your second question shows an unfamiliarity with basic Python syntax. request.POST is the parameter to the initialisation of the form instance.
I have to make some changes in a django project and though I'm familiar with python, I'm not with django.
This is my situation:
I have a table with the field "active". What I need to do is to let the users to sort the table based on the value of the field (yes/no).
I looked into views.py and I realized that there is a view that sorts the table based on the id:
users = User.objects.all().order_by('id')
My questions are:
How can I make the view to sort the table based on the url parameter?
Do I have to create another view or can I use the same with some kind of modifications?
You can use the same view.
def myView(request):
get_param = request.GET.get('my_param', 'id')
#some more processing
users = User.objects.order_by(get_param) #note - you dont need the `all()`
#rest of the code here.
That depends on what you want to render in your template. If both views will render similar html the it's probably better refactor them in a generic view and do the sorting depending on GET parameters.
I like separation of concerns, so another approach may be refactoring the view code inside a helper function and just call it inside each view with separated urls.
If the views doesn't render similar html and are different from each other in every aspect except that they sort something in some place, then I think the best way is to code both view separately.
It's just a consideration problem but answering your question you can get GET params accessing the request.GET or request.POST depending on the method.
Hope it helps!
So I'm currently working on a Django project where an object with an id is created from a form, and that id is needed in the form that the first form redirects to. Is there a way I can pass this id (in python) using redirect? I can't seem to figure out how to do this, but I'm sure there is a way since it seems like it would be pretty useful.
Thank you all,
Alex
Is there anyway to pass context variables to a redirect response? I want to redirect a user to a success page after they submit a form, but I don't want the success page to be just a static html file. I need to display extra information based on the form data.
I have looked at this question, but the solution presented there simply renders a different file at the same url. I'd like to redirect the user so that hitting refresh at the page won't submit duplicate entries into the application.
Right now the only thing I have been able to use with some success is redirecting to a url while passing it GET variables as described here. That just seems like a bit of a hack, and was just wondering if there is any better solution...
Thank You
The way I see it you have three options:
Use GET variables in the redirect.
Store something in the session.
If you are creating an object using the form that was submitted, put the id of that object in the redirect url and use it in the new view.
The limitation you are running up against is that http is stateless, not something inherent in django.
How about storing your values in a session, then have the redirected page pick up the values from there?