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.
Related
I have a CreateView class
where i set the success url like this
class ListCreateView(CreateView):
model = List
fields = "_all_"
success_url = "list"
after submitting the form
Its going to
http://127.0.0.1:8000/home/create_list/list
which doesnt exist
I want it to go to
http://127.0.0.1:8000/home/list
can anyone help with some suggestions? I tried reverselazy but that brings up another big error. and im a noob.
If you want to go to /home/list that should be your success_url.
And if you have named your views, you can do it by reverse_lazy().
Something like:
class ListCreateView(CreateView):
model = List
fields = "_all_"
success_url = "/home/list/"
# or if you named your urls in urls.py
success_url = reverse_lazy("home:list")
I have a CBV Listview for which I obtain and random queryset and paginate it, but, I see that there seems to a difference in content from somedomain.com/quiz/1 and somedomain.com/quiz/1/?page=1
My Listview looks like so:
class Test_ListView1(ListView):
template_name = "test.html"
paginate_by = 12
context_object_name = "test"
def get_queryset(self):
queryset = list(SomeModel.objects.order_by("-created_at").values('id','question_field','some_json'))
random.shuffle(queryset)
return queryset[:24]
in my urls, I cache my page so that it returns the same value for a certain period (50000sec) of time like so:
path('somedomain.com/quiz/1', cache_page(50000)(Test_ListView1.as_view()), name="test1" ),
but, I still see a difference between somedomain.com/quiz/1 and somedomain.com/quiz/1/?page=1 - they seem to be two different pages..
How do I make them the same?
According to this ticket Django takes into account query params when construct cache key. You can see source here. request.build_absolute_uri() is using here which returns full url with query params. That's why somedomain.com/quiz/1 and somedomain.com/quiz/1/?page=1 are different pages for cache middleware.
So to fix I suppose you can just add redirect if page query parameter not provided, something like this:
def get(self, request):
if not request.GET.get("page"):
return redirect(reverse('view_name') + '?page=1')
return super().get(request)
TLDR: I want to be able to provide slug in reverse_lazy('view', kwargs={'slug':'my_page'}) like this: reverse_lazy('view').apply(kwargs={'slug':'my_page'}), after creating the lazy object.
I have the following url pattern that includes a slug to identify a page model instance:
url(r'^(?P<slug>'+settings.SLUG_PATTERN+')/$', views.MyView.as_view(), name='view'),
I have another view for editing the page:
url(r'^(?P<slug>'+settings.SLUG_PATTERN+')/_edit/$',
views.MyEditView.as_view(success_url=reverse_lazy('view')), name='edit'),
Note the addition of success_url so that when I submit the form with the new content I'm redirected to the now-edited page. In case I ever change my view url pattern I don't have to worry about updating the redirect for my edit url.
After the form is validated and saved, the view grabs the success url to be used in a HttpResponseRedirect. However just the name 'view' isn't enough to identify the URL. I also need to know the slug name which is stored in my page model's slug field.
A similar question is here: success_url in UpdateView, based on passed value
The answers suggest writing a custom get_success_url for every view, but there must be better approaches.
In the generic views in django's edit.py there's this:
url = self.success_url.format(**self.object.__dict__)
If success_url were given as a hard coded URL but with a slug identifier such as '{slug}/' this would replace it with the slug field in my model. That's very close to what I want, but I don't want to hard code my URL. This brings me to my question:
How can I pass in parameters to a reverse_lazy object? I would use this in my base view's get_success_url with self.object.__dict__ and it'd just work everywhere.
Moreover if my slug string was stored on separate Slug model I might want the success URL to be '{slug.name}/'. With the above approach I could supply a mapping between the URL parameters and model attributes:
redirect_model_mapping = {'slug': '{slug.name}'}
...
def get_success_url(self):
url = self.success_url
if is_a_lazy_redirect(url):
url = url.somehow_apply_parameters(redirect_model_mapping)
return url.format(**self.object.__dict__)
I would like somehow_apply_parameters to be equivalent to originally calling reverse_lazy('blog:view', kwargs=redirect_model_mapping). However I don't think this should be in urls.py because it shouldn't have to know about the mapping.
This is a hack, but does what I want...
class MyView(FormMixin, ...):
#this is actually set on child classes
redirect_model_mapping = {'slug':'{slug.name}'}
def get_success_url(self):
url = self.success_url
if url is not None:
if hasattr(self.success_url, '_proxy____kw'):
url_parameters = dict((k, v.format(**self.object.__dict__)) for k, v in six.iteritems(self.redirect_model_mapping))
url._proxy____kw = {'kwargs': url_parameters}
url = force_text(url)
else:
url = url.format(**self.object.__dict__)
else:
raise ImproperlyConfigured("No URL to redirect to.")
return url
It replaces the kwards parameter normally passed to reverse_lazy but after it actually has the values it needs. As reverse_lazy also requires the string to match the regex, I had to make the mapping between url parameters and the values in the models first.
I'd quite like an approach that doesn't need to write to _proxy____kw.
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).
I have a model like this:
from wagtail.wagtailcore.models import Page
class Blog(Page):
created = models.DateTimeField(auto_now_add=True)
...
..
Right Now, my default, if my slug is hi-there, the blog post is accessible on site_url:/hi-there/ but I want it accessible via site:url/2014/02/05/hi-there/ The page has various methods like url, url_path which one should I override and what's the best practice to achieve something like this in wagtail?
The RoutablePageMixin is the current (v2.0+) way to accomplish this.
Add the module to your installed apps:
INSTALLED_APPS = [
...
"wagtail.contrib.routable_page",
]
Inherit from both wagtail.contrib.routable_page.models.RoutablePageMixin and wagtail.core.models.Page, then define some view methods and decorate them with the wagtail.contrib.routable_page.models.route decorator:
from wagtail.core.models import Page
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
class EventPage(RoutablePageMixin, Page):
...
#route(r'^$') # will override the default Page serving mechanism
def current_events(self, request):
"""
View function for the current events page
"""
...
#route(r'^past/$')
def past_events(self, request):
"""
View function for the past events page
"""
...
# Multiple routes!
#route(r'^year/(\d+)/$')
#route(r'^year/current/$')
def events_for_year(self, request, year=None):
"""
View function for the events for year page
"""
...
New in Wagtail v0.5 is a mechanism that directly addresses this sort of thing:
Embedding URL configuration in Pages or RoutablePage in v1.3.1
(the docs even have an Blog example!)
I was looking into migrating my blog to a wagtail version and wanted to support my previous url schema and had to solve this exact problem. Luckily I just found a solution and want to share it, hopefully this will be helpful for someone else in the future.
The solution is a 2 Step process.
change the url of a blog page to contain the date as well.
class Blog(Page):
created = models.DateTimeField(auto_now_add=True)
def get_url_parts(self, request=None):
super_response = super().get_url_parts(request)
# handle the cases of the original implementation
if super_response is None:
return None
(site_id, root_url, page_path) = super_response
if page_path == None:
return super_response
# In the happy case, add the date fields
# split prefix and slug to support blogs that are nested
prefix, slug, _ = page_path.rsplit("/", 2)
return (
site_id,
root_url,
f"{prefix}/{self.created.year}/{self.created.month}/{self.created.day}/{slug}/",
)
And now we need to make those posts also route-able.
class BlogIndexPage(RoutablePageMixin, Page):
...
def route(self, request, path_components):
if len(path_components) >= 4:
year, month, day, slug, *rest = path_components
try:
subpage = self.get_children().get(slug=slug)
return subpage.specific.route(request, rest)
except Page.DoesNotExist:
...
return super().route(request, path_components)
This solution ignores the date and just use the slug to locate a blog post as the original solution. This should also work when you don't use the RoutablePageMixin.
Hope this is still helpful for someone.