hey guys i am beginner in django
I want to generate url in django like this
http://something.com/searchpage/?page=12&color=red
currently in use this type of url
url(r'^searchpage/(?P<page>\d+)/(?P<color>\w+)$','home_product.views.search'),
plz help me
This looks like a GET request: just point your url to
url(r'^searchpage/$, 'home_product.views.search'),
and pull up the query terms from the request.GET directory in your views.py:
def search(request):
page = request.GET.get('page')
color = request.GET.get('color')
...
(Using GET.get, you will set missing values to None).
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'm new to python and scrapy.
I want to scrap data from website.
The web site uses AJAX for scrolling.
The get request url is as below.
http://www.justdial.com/functions/ajxsearch.php?national_search=0&act=pagination&city=Mumbai&search=Chemical+Dealers&where=&catid=944&psearch=&prid=&page=2&SID=&mntypgrp=0&toknbkt=&bookDate=
Please help me how I can use scrapy or any other python libraries
Thanks.
Seems like this AJAX request expects a correct Referer header, which is just a url of the current page. You can simply set the header when creating the request:
def parse(self, response):
# e.g. http://www.justdial.com/Mumbai/Dentists/ct-385543
my_headers = {'Referer': response.url}
yield Request("ajax_request_url",
headers=my_headers,
callback=self.parse_ajax)
def parse_ajax(self, response):
# results should be here
I am using django class based views. I am trying to validate the url field in forms.py file. To check whether the given url is valid format or not and return the errors in template page. Can anyone help me to do this.
def clean_website(self):
website = self.cleaned_data.get("website")
val = URLValidators(verify_exists=False)
val(website)
It's not working for me.Please any one help me to validate the field and return the error.
Thanks in advance.
try returning the data at the end of the method:
return val(website)
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.
Why? I want multiple models on the first level of the path :)
Using: Django 1.4.1
Code setup urls:
PAGE_SLUGS = '|'.join(Page.objects.values_list('slug', flat=True))
BRAND_SLUGS = ... same concept
(r'^(?P<brand_slug>%s)/$' % BRAND_SLUGS, 'novomore.apps.catalog.views.product_showcase_list'),
url(r'^%s/$' % PAGE_SLUGS, 'prefab.apps.pages.views.page_detail', name='page'),
In the save method of model Page:
if self.pk is None:
clear_url_caches()
I don't want to run a query on each request so thats why i use this aproach, when i add a instance the PAGE_SLUGS need to be updated.
clear_url_caches() doesnt seem to work
Any suggestions?
This doesn't do the trick:
if settings.ROOT_URLCONF in sys.modules:
reload(sys.modules[settings.ROOT_URLCONF])
reload(importlib.import_module(settings.ROOT_URLCONF))
From How to reload Django's URL config:
import sys
from django.conf import settings
def reload_urlconf(self):
if settings.ROOT_URLCONF in sys.modules:
reload(sys.modules[settings.ROOT_URLCONF])
return import_module(settings.ROOT_URLCONF)
I don't think what you're trying to do is a good idea. Why not simply allow any slug pattern in the URL regex, but return a 404 if you can't find the Page in question? That would have the same effect and be much simpler.
url(r'^(?P<slug>\w+)/$', 'prefab.apps.pages.views.page_detail', name='page'),
then your view code can do something like
from django import shortcuts
def page_detail(request, slug):
page = shortcuts.get_object_or_404(Page, slug=slug)
...