atkisai
this is dinamic variable and I dont know why I must add it add in all my templates links and add it all my views, this no logic
Luxmi
@login_required def wishlist(request): context = {} items = wishlist.objects.filter(user__icontains=request.user.id) context["items"] = items if request.method == "POST": rid = request.POST["rid"] is_exist = wishlist.objects.filter(prod__icontains=rid, user__icontains=request.user.id, status=False) if len(is_exist) > 0: context["message"] = "Item already exists" else: prod = get_object_or_404(RentAd, id=rid) user = get_object_or_404(User, id=request.user.id) w = wishlist(user=user, prod=prod) w.save() context["message"] = "{} added in your cart", format(prod.r_name) pass return render(request, 'wishlist.html', context)
Luxmi
Views
Luxmi
AttributeError at /wishlist/
Luxmi
'Wishlist' object has no attribute 'get'
Luxmi
They are fine the error is in views
Luxmi
Now I m getting a error as related field got invalid lookup
Luxmi
I just changes icontains to id
atkisai
I just changes icontains to id
okey😄 i bad now english and i dont understand you fix ot no
Manish
I just changes icontains to id
Why did you use icontains instead of id. id and pk are very powerful way to validate things
Luxmi
I tried using Id same error goes
Manish
I mean icontains instead of id...wow
Manish
You may use related_names and then you can use prefetch_related
Luxmi
I m a beginner can you please explain
Luxmi
Unsupported lookup I'd for autofield or join on the field not permitted When used I'd as icontains
Manish
Ok
Manish
If you have user object in your request object Just use Whishlist.objects.filter(user=request.user)
Fan / Ac
is it a bad practise in anyway if i use same view for login and logout which makes same url when login/logout
Ali
Hey, I wanna know how to implement a view that checks if the user is authenticated (if True, then raise 403 HTTP error otherwise, render the template) Any Idea?? I have already done this: if user.is_authenticated: raise PermissionError() else: return render(..) It actually works properly but I see a TraceBack error in my console..! Is it ok to raise an exception inside an IF statement??
マクスイム • グセブ
Hey, I wanna know how to implement a view that checks if the user is authenticated (if True, then raise 403 HTTP error otherwise, render the template) Any Idea?? I have already done this: if user.is_authenticated: raise PermissionError() else: return render(..) It actually works properly but I see a TraceBack error in my console..! Is it ok to raise an exception inside an IF statement??
There's a really elegant way to do this using function decorators ​def​ ​authenticated_user​(​view_func​): ​    ​@​wraps​(​view_func​) ​    ​def​ ​wrapper_func​(​request​, ​*​args​, ​**​kwargs​): ​        ​if​ ​request​.​user​.​is_authenticated​: ​            ​return​ ​view_func​(​request​, ​*​args​, ​**​kwargs​) else: raise PermissionDenied ​    ​return​ ​wrapper_func Then you will be able to use it in this way: @authenticated_user def some_view(request): return render(...) Oh, and BTW, there's already built-in Django decorator for unauthenticated users. from​ ​django​.​contrib​.​auth​.​decorators​ ​import​ ​login_required @​login_required​(​login_url​=​"login"​) def view_func(request): return render(...) Where login_url is URL name of path to login page.
Ali
cuse you did it in the wrapper_func right up there.
マクスイム • グセブ
Thanks. You mean it's elegant just because of raising inside an if statement right??
I mean that you don't actually have to manually check if user's authenticated in every view function, you can re-use the decorator in other view functions as well.
Ali
I want to restrict the authenticated users from accessing a view. That's all I want.
Ali
That's not functional.
Ali
I mean that you don't actually have to manually check if user's authenticated in every view function, you can re-use the decorator in other view functions as well.
I read somewhere someone said that there is no problem having this kind of traceback with full proper functionality at all. And noticed that the traceback accurse due to a flag.
マクスイム • グセブ
Well, average generic views enjoyer... from django.core.exceptions import PermissionDenied if not request.user.is_authenticated: raise PermissionDenied return render(...)
Ali
Well, average generic views enjoyer... from django.core.exceptions import PermissionDenied if not request.user.is_authenticated: raise PermissionDenied return render(...)
Actually rocking in with this style.. but having that kind of issue.. But the thing is, EVERYTHING IS WORKING FINE.
Ali
And authenticated users are now getting HTTP 403 error when trying to reach the endpoint btw.
マクスイム • グセブ
Actually rocking in with this style.. but having that kind of issue.. But the thing is, EVERYTHING IS WORKING FINE.
Idk, I'm weird but I always use functional style for Django views and generic views for Django Rest Framework
Ali
Idk, I'm weird but I always use functional style for Django views and generic views for Django Rest Framework
Dude class-based views are fire.. Any change you want, you jus override and DONE!!
Ali
You most likely know what I'm sayin.
マクスイム • グセブ
However, functional ones provide you more control and they are easier to read and understand
マクスイム • グセブ
Anyway, my personal preferences
Ali
However, functional ones provide you more control and they are easier to read and understand
Not sure 😅 I actually use them when I wanna render a static page like "about me" or something.
Ali
Pages that don't need any specifics or permissions or something..
マクスイム • グセブ
My last personal Django project at first was using Django templates but as project was getting bigger, I've realized that I want more dynamic behavior and I've added VueJS to my project dependencies. I import Vue in a base HTML template, so what's actually happening is my webpage is firstly rendered by Django and then some data renders with VueJS. 😐 I'm really thinking of completely separating frontend and backend of my project and just do DRF stuff with Django and serve frontend with Vue CLI but rewriting everything is a complete hell. Bruh. This idea is good but awful at the same time. I othen literally wake up in the middle of a night because of terrible dreams where I refactor my Django code 😨
Ali
Not sure even give it a go. I would implement a DRF service running through a container and a vuejs service running on another container and then, close all DJango container service ports. It will only interact with the Vuejs container and the Vuejs container is exposed to the world.
Ali
Also never experienced VueJS.
マクスイム • グセブ
マクスイム • グセブ
I mean, I had a good social life and I was happy before I started coding
Ali
I mean, I had a good social life and I was happy before I started coding
We all had.. :) And now, the life is became a dream.
Muhammad
How should I get start with Django because I am newbie
Ali
How should I get start with Django because I am newbie
Get advanced in Python first. Like get Pro in OOP. (So crucial) Then start reading the official docs. That helps a lot for newbies. Then checkout the YT project-based courses. You will get familiar with some concepts like Notification System, Authentication, Asyn and Sync Tasks, DRF, and etc. DjangoGirls walking-through docs is also well-written for newbies. Creates and deploys a simple personal Blog project.
Bart
Should i learn based view method ?
Ali
Should i learn based view method ?
you mean class-based views?
Ali
Yes
It gives you tens of features. why not?
Bart
I think it's more difecult
Rutherford
I think it's more difecult
Only if you don't have much knowledge about python classes and OOP in general
Bart
I'm good in oop
Rutherford
I'm good in oop
Then go through the source code for the various class based views. That's what I do before I use any of them.
Rutherford
Try again?
Read the code for the class based views. Say generic list view, read the source code on it and then you'll get to know what's happening.
Ali
Like what?
It does everything for you. You won't need to implement lots of things. Imagine you want to create a views that sends back all Post objects from the database as a context variable to a template. You are going to do something vital.. You are actually writing querysets in your views which is cool but tricky.. You gotta make sure your queryset is optimized enough to prevent any crashes, latency or something.. Class-based views just asks you a template name, model, and serializing mode (A list of all records, specific record, ..) and does that thing for you in a minute.
Ali
I think it's more difecult
That's difficult because you are less experienced in OOP I think. :)
Ali
Working with class-based views in Django is nothing but Overriding, Inheritance and some other concepts that I don't remember at the moment :)
Bart
Okay, I'll try it
Bart
Thanks guys
Ali
I have soved all oop problems in hacker rank
Maybe not familiar with Django class-based views. Like their structures.
Ali
Yeah i think that
Official docs would help. At least helped me. :)
Bart
I want videos
Bart
I can understand with practical explain
Ali
I don't like docs
Docs will show you the trick. Who can teach you like the Django creator?? 😁
Bart
I'll try it
Doragonsureiyā
How should I get start with Django because I am newbie
Looking for Django tutorials? you can follow these three recommended ones: * Official documentation and tutorial * Tutorial from MDN * Tutorial from django-girls
Ali
I'll try it
https://realpython.com/python3-object-oriented-programming/ https://docs.djangoproject.com/en/3.2/topics/class-based-views/