URL patterns for GET - django

I want to define url pattern for the below url and read these parameters in views
http://example.com/user/account?id=USR1045&status=1
I tried
url(r'^user/account/(?P<id>\w+)/(?P<status>\d+)/$', useraccount),
In Views
request.GET ['id']
request.GET ['status']
but it is not working, please correct me.

django url patterns do not capture the query string:
The URLconf searches against the requested URL, as a normal Python
string. This does not include GET or POST parameters, or the domain
name.
For example, in a request to http://www.example.com/myapp/, the
URLconf will look for myapp/.
In a request to http://www.example.com/myapp/?page=3, the URLconf will
look for myapp/.
The URLconf doesn’t look at the request method. In other words, all
request methods – POST, GET, HEAD, etc. – will be routed to the same
function for the same URL.
So, with that in mind, your url pattern should be:
url(r'^user/account/$', useraccount),
In your useraccount method:
def useraccount(request):
user_id = request.GET.get('id')
status = request.GET.get('status')
if user_id and status:
# do stuff
else:
# user id or status were not in the querystring
# do other stuff

Querystring params and django urls pattern are not the same thing.
so, using django urls pattern:
your url:
http://example.com/user/account/USR1045/1
urls.py
url(r'^user/account/(?P<id>\w+)/(?P<status>\d+)/$', views.useraccount)
views.py
def useraccount(request, id, status):

Related

django url _reverse _ not a valid view function or pattern name

the redirect url is
"liveinterviewList/2"
and, ofcourse, I declare that url in url.py
more over, when I type that url in browser manualy, it works well.
what's the matter?
more question.
at this case, I write the user_id on the url.
I think, it is not good way to make url pattern.
but I don't know how I deliver the user_id variable without url pattern.
please give me a hint.
What HariHaraSudhan left out was how to use parameters. For your case, you would want something like:
path(r'liveinterviewList/<int:userId>', ..., name='live-interview'),
And then when you are ready to reverse, use this:
reverse('app:live-interview', kwargs={ 'userId': userId })
where app is the name of the app in which your view lives. If your url lives in the main urls file , you don't need the app: prefix.
Django reverse function accepts the name of the path not the URL.
lets say i have url patterns like this
urlpatterns = [
path('/users/list', name="users-list")
]
In my view i can use like this
def my_view(request):
return redirect(reverse("users-list"));
You should add a name to your path url and use it to redirect.
As the django doc says :
urls :
urlpatterns = [
path('/name', name="some-view-name")
]
def my_view(request):
...
return redirect('some-view-name')

my url is url(r'^login/(?P<email>\w+#\w+\.\w+)$', 'login'), how can I get the url as /login

well, if my request path is 127.0.0.1:8000/admin/user/edit/10, then I want to check whether the current user A has the permission to access the /admin/user/edit, when I use url = request.path, I get the url as /admin/user/edit/10.
But I want to get url = '/admin/user/edit' instead url = '/admin/user/edit/10'.So how can I get the correct url??much appreciate!!
Why don't you use the permission_required decorator above your view?
#permission_required('app.edit_right') # Fill in your permission
def edit_user(request):
#do stuff here
If the user (or the groups he's in) doesn't have this permission, then he will be redirected. Look for more info on the Django docs

directing based on GET request parameters in django

I'm in c.html (http://localhost:8000/practice/c) and I click a link that changes my url to
http://localhost:8000/practice/c/?q=1
this is urls.py
url(r'^$', prac_c),
url(r'^q=\d',fetch_c),
and this is views.py
def prac_c(request):
return render_to_response('subject/c.html', {"problem_list":problem_list})
def fetch_c(request):
qno = request.GET.get('q')
if qno:
return render_to_response('subject/que.html', {'qno':qno}, context_instance=RequestContext(request))
but I'm not getting directed to que.html. Something wrong with the urls.py?
What the URLconf searches against
The URLconf searches against the requested URL, as a normal Python string. This does not include GET or POST parameters, or the domain name.
source

Django URL pattern (~~/?item_id=2)

https://xxxx/category_check_view/?item_id=2
Above is a sample of URL pattern. How should i configured my URL in order to enable it to redirect to the right view?
I seem to get it working for a url like this https://xxxx/category_check_view/2/ only so far.
You can pass parameters to a view either in the url:
/category_check_view/2
Or via GET params:
/category_check_view/?item_id=2
GET params are not processed by the URL handler, but rather passed directly to the GET param dict accessible in a view at request.GET.
The Django (i.e. preferred) way to do handle URLs is the first one. So you would have a URL conf:
(r'^category_check_view/(\d{4})$', 'proj.app.your_view'),
And a matching view:
def your_view(request, id):
obj = Obj.objects.get(id=id)
# ...
However, if you insist on passing the param via GET you would just do:
(r'^category_check_view$', 'proj.app.your_view'),
And:
def your_view(request):
id = request.GET.get('item_id')
obj = Obj.objects.get(id=id)
# ...
You can't use get parameters in URL pattern. Use them in your view:
item_id = request.GET.get('item_id')

django url regex doesn't match

Not sure why this doesn't match any urls in urls.py. I checked with a regex checker and it should be correct.
urls.py:
url(r'^toggle_fave/\?post_id=(?P<post_id>\d+)$', 'core.views.toggle_fave', name="toggle_fave"),
sample url:
http://localhost:8000/toggle_fave/?post_id=7
Checked using this simple regex checked. Seems right. Any ideas?
Thanks!
the urlconf isn't used to match the request.GET parameters of your url. You do that within the view.
you either want your urls to look like this:
http://localhost:8000/toggle_fave/7/
and match it using:
url(r'^toggle_fave/(?P<post_id>\d+)/$', 'core.views.toggle_fave', name="toggle_fave"),
with your view that looks like:
def toggle_fave(request, post_id):
post = get_object_or_404(Post, pk=post_id)
...
or
http://localhost:8000/toggle_fave/?post_id=7
and your urls.py:
url(r'^toggle_fave/$', 'core.views.toggle_fave', name="toggle_fave"),
and views.py:
def toggle_fave(request):
post_id = request.GET.get('post_id', '')
if post_id:
post = get_object_or_404(Post, pk=post_id)
...