Questions tagged [django-request]
96 questions
67
votes
7 answers
How to get URL of current page, including parameters, in a template?
Is there a way to get the current page URL and all its parameters in a Django template?
For example, a templatetag that would print a full URL like /foo/bar?param=1&baz=2

Matt Hampel
- 5,088
- 12
- 52
- 78
9
votes
1 answer
How to safely access request object in Django models
What I am trying to do:
I am trying to access request object in my django models so that I can get the currently logged in user with request.user.
What I have tried:
I found a hack on this site. But someone in the comments pointed out not to do it…

Ahtisham
- 9,170
- 4
- 43
- 57
6
votes
1 answer
Add a custom header on request in a Django middleware
I want to implement a middleware in django, that will append a header on the request's existing headers, before the get_response(request) function.
Though, when trying like this:
request.headers['CUSTOM_HEADER'] = 'CUSTOM_VALUE'
I get an error:…

angelos_lex
- 1,593
- 16
- 24
5
votes
2 answers
get domain name from django request
I had a website with domain www.example1.com, and recently i have pointed another domain dns www.example2.com to same server that contains www.example1.com, so both www.example1.com and www.example2.com serves the same code but domain name should be…

Shiva Krishna Bavandla
- 25,548
- 75
- 193
- 313
4
votes
2 answers
Django How to check a request is Ajax
The HttpRequest.is_ajax() method is deprecated as it relied on a jQuery-specific way of signifying AJAX calls, while current usage tends to use the JavaScript Fetch API. Depending on your use case, you can either write your own AJAX detection method…

Ayaz Ur Rashid
- 221
- 3
- 9
4
votes
1 answer
How to filter objects at django based on related field?
I have models:
Product
Store
ProductStore (additional table with foreign keys to Store and Product, also Boolean 'enabled', and stock(integer) )
The questions:
How can I filter Products which has Enabled=True for current store__id (from…

Taras
- 447
- 2
- 7
- 18
4
votes
3 answers
get current user for 'created_by' field in model class
I'm currently working on a Django app, and I'm trying to set the current user as default on a model, but it doesn't work.
created_by = models.ForeignKey(User, default=request.user, null=True, blank=True, on_delete=models.DO_NOTHING,…

Theophile
- 105
- 1
- 1
- 8
4
votes
1 answer
Modify Djangorestframework response structure
I am met with a situation where my frontend guy needs the response in the following format from all of my endpoints.
{
status: 200,
message: "OK",
content: {Normal DRF Response Body}
}
I know that I can get this structure by using the…

Huzaifa Qamer
- 314
- 2
- 4
- 17
3
votes
1 answer
Django get request.POST.get() parameter not working as expected, parameter name with brackets[]
I have a code that is behaving really strange. The view receives a POST request with a key "tags[]", it is a list. I need to get that list but request.POST.get() only returns the last item of the list. This is the code:
....
elif request.method ==…

Alejandro Veintimilla
- 10,743
- 23
- 91
- 180
3
votes
1 answer
Append a new Http header using django request.META
i am using django-rest framework and i am able to get and set the custom headers using the below META information,
class log_middleware:
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and…

Naggappan Ramukannan
- 2,564
- 9
- 36
- 59
2
votes
2 answers
How to send POST request after form submit 302 in Django?
I have an implementation as follows:
There is a payment form wherein user fills all the details.(API1), here I'm getting an error 302:
On submit of that form one of the function in my views is called.
In the backend implementation ie. in…

mach2
- 450
- 2
- 6
- 24
2
votes
2 answers
django handling basic http auth
Currently I have the following code to handle an incoming GET request:
#view.py
def handle_request(request):
if request.method == 'GET':
return response
this code can handle a simple GET request the form:
curl…

D_M
- 87
- 10
2
votes
0 answers
Django data handling in custom middleware
In my django webapp, I have to pass user data param(udp) in a request and return the same in response without modifying it in view.
Request
{
"mobile": "111111111",
"udp":[
{
"key": "keyName",
…

user2858738
- 520
- 4
- 15
2
votes
3 answers
What does "resolve_variable" do in Django? ("template.Variable")
What does resolve_variable do? And could I use it for accessing the request outside of the view?
Edit
So template.Variable is the correct way to go - but I'm still unsure of its purpose. The documentation doesn't really help.
Cheers guys.

Glycerine
- 7,157
- 4
- 39
- 65
1
vote
1 answer
List of arrivals, per year
I have these three models (I've summarize them):
class Tourist(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
class Etablissement(models.Model):
name =…

Etsè FIATSI
- 27
- 3