Im using the following statement to set Language Option in my projects which works as expected.
request.session['django_language'] = "de"
This is fine with in View, but when the control goes to other files to connect to DB or external services how can I access it. I dont want to pass the request object through out all the application.
If something like UserContext/RequestContext where every request has to go-through it (Middleware) so that I can set it there and access it without help if request object.
I understand from the headline that you want to store the language per user.
It might be best to extend the user model and add a model field for preferred_language.
Related
I have a use case where I need to show the data of the company the user belongs to. I don't want the url to show something like: 127.0.0.1:8000/api/document?company=somecompany rather I want to pass the company within the header and return the data related to the company.
Is there any way to achieve this in Django REST Framework? Else, how can I avoid 127.0.0.1:8000/api/document?company=somecompany.
You can use request.META.get('NameOfYourHeader') and set the custom header from the frontend.
Now be aware that this will real bad practise and query params are meant for this. Also if you want to filter on some user, there may be some workarounds like request.user or nested serializer from a user instance.
I'm kind of a noob with Django. The thing is, I'm trying to create a "community" website (used by different people), and every user can create/edit project, and when they do so, it creates an event (alerting every user that this user has edited/created a project).
The thing is, I'm using angular's , and when I create a project while logged in, I have no clue about how to set project.user (the creator of the project) with the value request.user. Same thing for events, when I create one, I don't know how to assign event.user to request.user.
I create my objects in my javascript file using $resource, posting them in my REST API.
I don't know if I'm very clear... If you need any details or code just let me know !!
I have a Django project using Tastypie as its main API and it works ok. But now I want to be able to receive images through the API (coming from phones and such). It looks like Tastypie is not quite ready yet in that field. I'm ready to try something else just for that matter, or even write a custom view. How can I do that?
A standard Django view can technically serve as an API endpoint, too, so why not just write a view that handles a POST payload?
You could even make a form which you use to validate the input, even if your client device isn't using that form to capture content - as long as the fields and their criteria match up, it'll still fit.
I have made a custom manager by creating a class inheriting from models.Manager.The manager just changed the default model.objects query to add some filters. Now, I want to add a filter according to the user logged in. I dont want to have to search through code changing what params are added, is there any way I can get the request object/current user without passing it through to the method?
Im hoping this is not a stupid question, but I may just be getting confused...
This is the basic setup of the Manager
class pubManager(models.Manager):
def get_queryset(self):
return pubEnt.objects.filter(state='new')
def on_site(self):
return pubEnt.objects.filter(state='old', val=0)
There is no way in django to access the current request without passing it. If can't live without it you should probably rethink your design! Having access to the request shouldn't be a requirement of a manager's method, since it could also be accessible from somewhere where you do not have a request object (think for example of calling the method from the python shell). If you need access to the currently logged-in user, pass the user object to the method (from request.user), but not the whole request!
Global Django requests
http://nedbatchelder.com/blog/201008/global_django_requests.html
I'm using django-registration to log users into my application. That part works fine. The part that I cannot figure out is how to set custom session variables when the user logs in. For instance, I'd like to populate variables containing UserProfile data as well as the output of a few other functions. Then I'd be able to use that information in subsequent views/templates.
If anybody can point me to a tutorial online or post some sample code, that would be great.
I'm using django 1.1 and Python 2.6
If you don't want persistent storage of user data (just additional session data) have a look at:
http://docs.djangoproject.com/en/dev/topics/http/sessions/
The sessions framework will most probably be already enabled if you use django.contrib.auth.
If you want persistent storage of additional user data (not only in a session, but in the database), you will store them in another "profile" model:
http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
I realize #stefanw provided you an alternative solution, but to answer the original question:
Setting session data at login is difficult because the easiest place to set that data is in your view function, and the particular view function you'd want to modify is a part of the contrib.django.auth app.
So your options would be the following:
Create a small middleware class to set your session data.
Create a template tag or other bit of code than can be integrated into the login template or subsequent page that will set the data you want.
Write your own custom login view function (it's really quite easy, actually).
Happy django-ing!