django-views

middleware updates request.user, but request.user becomes AnonymousUser in view

元气小坏坏 提交于 2021-02-17 06:38:48
问题 I wrote a simple JWT middleware to get user from the JWT. The method get_user_from_jwt returns a User object. # app.middlewares.py class JwtMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): self.process_request(request) return self.get_response(request) def process_request(self, request): request.user = self.get_user_from_jwt(request.headers) def get_user_pk_from_jwt(self, auth_header: str) -> int: _, token = auth_header.split(' ')

django How to execute a function asynchronously i.e, handover a task to a sub process and return response

天大地大妈咪最大 提交于 2021-02-13 05:37:50
问题 I am using django 2.0 and python 3.6. The user registration includes sending of a verification mail. And this mail sending process is taking longer and the user is kept waiting. What I need?: If the user registration form is valid, mailing details are sent to another task handler and regardless of whether the mail is sent or not, the function must resume and return a response. def new_user_registration(request): form = CustomUserCreationForm(request.POST or None) if form.is_valid(): user =

Django Aggregate Query Include Zero Count

末鹿安然 提交于 2021-02-11 16:11:27
问题 In my Django application, I'm trying to get a Count of all Student submitted Paper s, including students who have submitted NO papers (represented as count=0). models.py class Student(models.Model): idstudent = models.AutoField(primary_key=True) student_name = models.CharField(max_length=250, null=False, blank=False, verbose_name='Student Name') class Paper(models.Model): idpaper = models.AutoField(primary_key=True) student = models.ForeignKey(Student, on_delete=models.PROTECT, null=False,

Django 3.0: Reverse for 'product' with no arguments not found. 1 pattern(s) tried: ['product/(?P<slug>[^/]+)/$']

痞子三分冷 提交于 2021-02-11 16:01:47
问题 I have model named Book in models.py file. And this model has slug field to display details of books Books are being displayed in home.html template and product.html template is to display details of selected book. I really don't know much about slugs, and how they work. Models.py: class Book(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) title = models.CharField('Title', max_length=255) authors = models.ManyToManyField(Author, related_name='books

Django 3.0: Reverse for 'product' with no arguments not found. 1 pattern(s) tried: ['product/(?P<slug>[^/]+)/$']

亡梦爱人 提交于 2021-02-11 15:59:40
问题 I have model named Book in models.py file. And this model has slug field to display details of books Books are being displayed in home.html template and product.html template is to display details of selected book. I really don't know much about slugs, and how they work. Models.py: class Book(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) title = models.CharField('Title', max_length=255) authors = models.ManyToManyField(Author, related_name='books

No User matches the given query. Page Not found 404?

馋奶兔 提交于 2021-02-11 14:59:53
问题 In my blog website I want to open another user's profile on clicking one of the links. But it keeps on showing 404 error saying No user matches the given query. Here's that link in a base.html <a href="{% url 'blogapp:userprofile' username=view.kwargs.username %}">{{ view.kwargs.username }}</a> Here's my urls.py pattern for the function- path('userprofile/<str:username>/',views.userprofile,name='userprofile'), Here's my function views.py @login_required def userprofile(request,username): user

Multi Nested with Django Restframework and Mongodb

落花浮王杯 提交于 2021-02-11 14:32:36
问题 I want output like this. I have a task to complete more than 5 models with nested type and all CRUD methods by using RestFawork.If anyone knows the answer share with me. { "id": 1, "first_name": "John 1", "last_name": "Robert 1", "instrument": "KeyBoard", "album_musician": [ { "id": 3, "artist": 1, "name": "Test robert", "release_date": "2020-09-16", "num_stars": 400 "Language": [ { "Original": "English", "Dubbed": "Tamil" } ] } ] } In this Code, I did only Sigle Nested Framework. My model.py

Django - Messages functionality not working in Class based view

十年热恋 提交于 2021-02-11 14:02:43
问题 Unable to display messages in class based view. In another app's views.py, it is working fine where I used function based view. views.py: class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, SuccessMessageMixin, DeleteView): model = Post success_url = '/user-profile/' success_message = "Your post has been deleted sucessfully!" def test_func(self): post = self.get_object() if self.request.user == post.author: return True return False urls.py: path('user-profile/', user_views.user

How to make sum query with type casting and calculation in django views?

孤街醉人 提交于 2021-02-11 13:50:10
问题 I'm calculating the sold items cost in django views and in django signals, and I want to calculate sold items cost on the fly. Price and quantity fields are integers. How can I convert one of them to the float and make sum query with some calculations like these sql queries below? SELECT sum((t.price::FLOAT * t.quantity) / 1000) as cost FROM public."sold" t; SELECT t.id, t.price, t.quantity, sum((price::FLOAT * quantity) / 1000) as cost FROM public."sold" t GROUP BY t.id; EDIT: Of course

Number of online people in django [closed]

独自空忆成欢 提交于 2021-02-11 12:15:40
问题 Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . Improve this question I have a website and i want to find number of online users in my website(All of users logged to their account or not): And when they get offline or close page the number of online users get update: How can i do that?is there any library to do that? 回答1: You can