Firstly, sorry if the question is so obviously (I'm quite newbie). I have a Django project where the user can add or remove, for example, interests to his/her profile (ManyToMany relationship). I've achieve this with 3 views. The first one where the profile's interests are rendered, the second one to add (nothing is rendered, when the user click a link, the view just update the profile and return the first view, the one with the profile's interests) and the third one to remove interest.
urls.py
path('home/overview/add/<int:interest_pk>', add_view, name="add_view"),
views.py
def add_view(request,interest_pk): #the third (the one to remove) view is similar
user = request.user
user.profile.interests.add(Category.objects.filter(pk= category_pk).get())
return redirect('overview') #Overview is the view where the interests are rendered
This works but now I'd like to improve this making it asynchronously. So here is my question, should I use Django Rest Framework to handle this kind of request (the ones related to the POST and DELETE methods) or is there any other method?
Note: I'm not using forms to send the information (I'd rather not to use it because it's simpler to send in an url the pk of the interest and handle it in a view) and I think it could be achieved using only requests. (Correct me if I'm wrong)
Related
I want to build a django application where the users are workers in a callcenter. When they talk to a client they have a specific task, but at some point they optionally might need to update the client's contact data.
The point is that there already is a view for updating the contact data. The only thing that would need to change (at the moment) is the URL you get send to after submit/cancel. So I am wondering whether I should reuse that view. I can think of roughly three options:
Reuse the view and pass the return URL as parameter
Create a new view that reuses parts of the existing view (e.g. template)
Create a completely new view
What would you recommend and why?
Option number one seems to be fine, because it reuses a lot of code. But I think it'd be a bit better to not to send such URL by parameter (an advantage that I can see: user wouldn't know the redirect URL if POST is failed).
If there are just 2 options where the user is redirected, you could consider sending boolean parameter, sth like url?during_call=1. And then in your view you could add a small ifology:
def view(request):
# your logic here
during_call = request.POST.get('during_call', False)
redirect_url = 'url1' if during_call else 'url2'
return redirect(redirect_url)
This is probably a very newbie question however I'm kind of stuck.
I've been looking around for a way to render a user based view for a calendar scheduler however I can't seem to find anything about user-based views in django. It's probably really simple but I'm too blind to spot any documentation about it. Something alongside of a user profile.
I've read through the django book but I cannot recall if there was anything that describes what I'm trying to achieve. Any hints or links would be a great help.
Thank you.
What you are actually are looking for is a DetailView, Django nowdays uses Class Based Views which are kind of self explanatory, there is a ListView which is used to display a list of objects, a DetailView which is used to display details of a specific object and others like FormViews, CreateView, DeleteView etc (you can find more information to this very detailed site apart from Django's own documentation):
http://ccbv.co.uk/
Since you want to display a view for a specified object, this becomes a DetailView, since a User also has a calendar with events, then you also need to fetch the Calendar with the events (but this all breaks down to how your model relations are glued together).
For instance if your user has a Single Calendar (he...should actually) and many events in the calendar, you could declare a Queryset in the DetailView:
http://ccbv.co.uk/projects/Django/1.6/django.views.generic.detail/DetailView/
You can create common template for all users and then populate it with user specific data.
I'm not really sure where you're stuck, or what you're defining as a "user-based view".
The user model is like any other: you can query it, and other models can be related to it. If you just want to show data for the current user, you get that from request.user: otherwise, you can pass the user ID in the URL like any other parameter.
Just curious. What is the best practice for when to use form vs model validation?
From what I understand currently, form validation should be used for:
AJAX / HTTP requests params
Forms that do not correlate to a model?
Another question is: I have a HTML form that roughly correlates to a model instance, do I use a ModelForm for it?
Definitely use ModelForm, if your form resembles model object even in a tiny bit.
If there are some minor differences (e.g. you don't use some of the fields or you want to use different error messages etc.) it's much easier to customize ModelForm then to use Form and implement all this functionality from scratch.
For more reference regarding ModelForm please checkout PyDanny's Core Concepts of Django ModelForms.
I am also trying to understand what is the difference/relation between form and model validation and I would like to share my notes that are formed after reading several docs.
I am currently interested in Creating Forms from Models
#mariodev shared the document Core Concepts of Django ModelForms and this provided a good start.
ModelForms select validators based off of Model field definitions
The main story behind the scenes seems to be the DRY principal. This article explains very well what exactly is the case here.
All right, all this is fair. The question is "Where in the Django Documentation is this explained"?
I bumped on a very brilliant article where it states that:
The form.full_clean() method is called by django at the start of the validation process (by form.is_valid(), usually immediately after the view receives the posted data).
Correct me if I am wrong but that line reads that everytime I enter data and hit 'enter' the validation process begins!
OK, this is simple now:
The validation on a ModelForm begins when we hit 'enter'.
Django first validates the form by checking one by one every applicable validation method on Fields, Field Subclasses (This is the documentation for a model's field subclass, not for a form field subclass), Form Subclasses and ModelForm (since it is a ModelForm).
Finally, it validates the Model Instance.
This is how all this works theoretically. The only thing that remains is to implement it.
My web app is built on top of Pinax. My problem has two parts.
Part one
The default LoginForm does not provide a reCaptcha field. To check brute-force attempts I would like to include one, but for this I would need to supply my own sub-class of LoginForm.
From the source it seems like I can pass form_class as extra options to the included urls conf, but then this would set my version of form as the form_class for signup view too. That is unfortunate. Is there any other way?
Part two
Second part of the problem is that I want to enhance the user experience. So, I would like to present the reCaptch field only when the users fails to authenticate for the third consecutive times. For this to work, I am considering keeping a failed attempt counter in the user session. The problem is how do get reference to session object from the __init__() of the form?
Is there anyway I can achieve this without modifying Pinax code?
I'm going to be honest: this is a question I asked on the Django-Users mailinglist last week. Since I didn't get any replies there yet, I'm reposting it on Stack Overflow in the hope that it gets more attention here.
I want to create an app that makes it easy to do user friendly,
multiple / mass file upload in your own apps. With user friendly I
mean upload like Gmail, Flickr, ... where the user can select multiple
files at once in the browse file dialog. The files are then uploaded
sequentially or in parallel and a nice overview of the selected files
is shown on the page with a progress bar next to them. A 'Cancel'
upload button is also a possible option.
All that niceness is usually solved by using a Flash object. Complete
solutions are out there for the client side, like: SWFUpload
http://swfupload.org/ , FancyUpload http://digitarald.de/project/fancyupload/
, YUI 2 Uploader http://developer.yahoo.com/yui/uploader/ and probably
many more.
Ofcourse the trick is getting those solutions integrated in your
project. Especially in a framework like Django, double so if you want
it to be reusable.
So, I have a few ideas, but I'm neither an expert on Django nor on
Flash based upload solutions. I'll share my ideas here in the hope of
getting some feedback from more knowledgeable and experienced people.
(Or even just some 'I want this too!' replies :) )
You will notice that I make a few assumptions: this is to keep the
(initial) scope of the application under control. These assumptions
are of course debatable:
All right, my idea's so far:
If you want to mass upload multiple files, you are going to have a
model to contain each file in. I.e. the model will contain one
FileField or one ImageField.
Models with multiple (but ofcourse finite) amount of FileFields/
ImageFields are not in need of easy mass uploading imho: if you have a
model with 100 FileFields you are doing something wrong :)
Examples where you would want my envisioned kind of mass upload:
An app that has just one model 'Brochure' with a file field, a
title field (dynamically created from the filename) and a date_added
field.
A photo gallery app with models 'Gallery' and 'Photo'. You pick a
Gallery to add pictures to, upload the pictures and new Photo objects
are created and foreign keys set to the chosen Gallery.
It would be nice to be able to configure or extend the app for your
favorite Flash upload solution. We can pick one of the three above as
a default, but implement the app so that people can easily add
additional implementations (kinda like Django can use multiple
databases). Let it be agnostic to any particular client side solution.
If we need to pick one to start with, maybe pick the one with the
smallest footprint? (smallest download of client side stuff)
The Flash based solutions asynchronously (and either sequentially or
in parallel) POST the files to a url. I suggest that url to be local
to our generic app (so it's the same for every app where you use our
app in). That url will go to a view provided by our generic app.
The view will do the following: create a new model instance, add the
file, OPTIONALLY DO EXTRA STUFF and save the instance.
DO EXTRA STUFF is code that the app that uses our app wants to run.
It doesn't have to provide any extra code, if the model has just a
FileField/ImageField the standard view code will do the job.
But most app will want to do extra stuff I think, like filling in
the other fields: title, date_added, foreignkeys, manytomany, ...
I have not yet thought about a mechanism for DO EXTRA STUFF. Just
wrapping the generic app view came to mind, but that is not developer
friendly, since you would have to write your own url pattern and your
own view. Then you have to tell the Flash solutions to use a new url
etc...
I think something like signals could be used here?
Forms/Admin: I'm still very sketchy on how all this could best be
integrated in the Admin or generic Django forms/widgets/...
(and this is were my lack of Django experience shows):
In the case of the Gallery/Photo app:
You could provide a mass Photo upload widget on the Gallery detail
form. But what if the Gallery instance is not saved yet? The file
upload view won't be able to set the foreignkeys on the Photo
instances. I see that the auth app, when you create a user, first asks
for username and password and only then provides you with a bigger
form to fill in emailadres, pick roles etc. We could do something like
that.
In the case of an app with just one model:
How do you provide a form in the Django admin to do your mass
upload? You can't do it with the detail form of your model, that's
just for one model instance.
There's probably dozens more questions that need to be answered before
I can even start on this app. So please tell me what you think! Give
me input! What do you like? What not? What would you do different? Is
this idea solid? Where is it not?
Thank you!
I just released a simple app for this about a month ago: django-uploadify.
It's basically a Django template tag that acts as a wrapper for the very nifty Uploadify (requires jQuery). Using it is as simple as adding this to your template...
{% load uploadify_tags }{% multi_file_upload ‘/upload/complete/url/’ %}
The tag will fire events (1 per file) on both the client-side and server-side (Django signal) to indicate when an incoming file has been received.
For example, assuming you have a model 'Media' that handles all user-uploaded files...
def upload_received_handler(sender, data, **kwargs):
if file:
new_media = Media.objects.create(
file = data,
new_upload = True,
)
new_media.save()
upload_recieved.connect(upload_received_handler, dispatch_uid=‘whatever.upload_received’)
Check out the wiki for info on how to set it up and create the signal handlers (client/server).
About your conceptual implementation from above, here's a few points of consideration:
Having the app automatically create the "File Model" instance probably isn't as robust as people may already have their own models they're working with
If you want to implement any type of security or authentication, you need an open system and less of an 'auto-create' type
I really think signals/events are the way to handle this, and also handle the 'DO OTHER STUFF' part of what you mentioned.
My conclusion was that multi-upload can never really be a form widget in the sense that Django implements form widgets. 1 file will most likely be represented by 1 model instance (with some exceptions), which means that we end up with a situation where 1 widget can represent N model instances. However Django is setup so that a widget represents 1 value for 1 field in 1 instance. It just doesn't fit for the majority of use-cases to have it as a widget (hence why I went the template tag route).