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)
...
Related
sorry for my noob question as I am just starting to learn Django. I would appreciate if someone could tell me how can i change data dynamically on page in django. Let me clear this:
What I want:
When url is http://localhost/data/1111, page data should be like data is 1111.
When url is http://localhost/data/2222, page data should be like data is 2222.
What I did:
def index(request):
print(int(request.GET["data"])) # for debugging only
return HttpResponse("data of Page")
and url was:
path('<int:data>', views.index, name='index')
Since you have a value in your url, the <int:data> part, that needs to be captured by the view, which means your view function has to be aware of the extra parameter.
So, the correct view for this would be:
def index(request, data):
print(data) # data is already converted to int, since thats what you used in your url
return HttpResponse(f"data is {data}")
Since you don't want to pass query parameters in your url, change your url to look like this
path('data/', views.index, name='index')
def index(request):
print(int(request.GET["data"])) # for debugging only
return HttpResponse("data of Page")
Note that on GET['data'], data is not what is on the url pattern but rather it should be a input name on the form like <input name='amount' type='number' />
Now you can access amount like this
def index(request):
print(int(request.GET["amount"])) # for debugging only
return HttpResponse("data of Page")
I am trying to use Django's pagination for class based views, as described in the docs.
In my urls.py, I have:
url(r'^clues/page(?P<page>[0-9]+)/$', views.ClueIndexView.as_view())
The docs tell me I should be able to access this with an url like:
/clues/?page=3
But that always fails with a 404.
Instead, /clues/page3/ works....but that isn't what I want...I want to use ?page=3.
What am I doing wrong?
EDIT:
I'm handling it with a class-based view, like so:
class ClueIndexView(ListView):
context_object_name = 'clue_list'
template_name = 'clue_list.html'
queryset = Clue.objects.all()
paginate_by = 10
You should do something like this:
url(r'^clues/$')
def clues(request):
if request.method == 'GET':
page = request.GET.get('page')
...
all GET info passed after '?' like your page '?page=n' stored in request.GET dictionary
My url was bad. I found the docs to be a bit confusing. My url needs to be just
url(r'^clues/$', views.ClueIndexView.as_view()
Works now.
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):
I'm currently experimenting with Django and creating apps following the tutorials on the official website.
So my urls.py looks like:
urlpatterns = patterns('',
(r'^/$','ulogin.views.index'), #why doesn't this work?
(r'^ucode/$', 'ulogin.views.index'),
(r'^ucode/(\d+)/$', 'ulogin.views.index'),
)
And my views.py looks like:
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
def redirect_to_index(request):
return HttpResponseRedirect('/ucode/')
When I run the server to check the test url, http://127.0.0.1:8000/ucode displays "Hello, world...etc" correctly, and works just fine. I've been messing with urls.py but I don't know how to get http://127.0.0.1:8000/ to display ulogin.views.index.
First of all, for the pattern to match
(r'^/$','ulogin.views.index')
Should be
(r'^$','ulogin.views.index')
Also, trying to match the following URL would raise errors
(r'^ucode/(\d+)/$', 'ulogin.views.index'),
because there is no view method that takes \d+ as a parameter.
The fix i recommend is:
(r'^ucode/(<?P<id>[\d]+)/$', 'ulogin.views.index'),
and then
def index(request, id=None):
return HttpResponse("Hello, world. You're at the poll index.")
You can read more about named URL groups here
It does not work because the root of a web-server when it comes to django urls is characterized by the empty string, like so -> ^$. So, just change ^/$ to ^$, and it will work.
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