I am making a page that shows the list of post, which was in my blog, but the problem is, I just want to show 3 posts each page, if user clicks next, they can see the next 3 posts, or click previous to see the previous page. How can I do that? if the code is too long, you can just tell me how can I do that. I am thinking of using loop, but with previous function is a bad idea. Thanks for your reading and hope to see your reply soon
Use django pagination. If you use generic ListView then pagination will be done automatically by providing paginate_by parameter.
Related
I am trying to make a simple e-commerce website and followed some tutorials.
However, the author of the book used complicated function based view to make cart function..
there are bunch of session stuffs.. and I don't understand the logic..
and I am trying to think the other way..
what about using database to store all the cart related data, and
use CBV to build it?
for example,
CartListView to see the contents of the cart, and CartUpdateView to change the quantity..
then are they going to be two different pages? separated page that user should go to the
different page to change the value??
please help me T T
You can access the session in any sort of CBV as self.request.session and a "shopping cart" is normally stored therein.
You'll certainly need to implement a CartListView to see what's in it, or possibly a CartEditView to show the cart with options to edit the quantities and delete anything that shouldn't be in there.
Adding products to the cart may well be an "Add" button on a ProductDetailView or lots of add buttons in a ProductListView. You might add a POST handler method to these views which are otherwise read-only (GET-only) bt default. Or you might make them FormViews, even though the form would be hidden and filled/POSTed by JS rather than the shopper doing anything other than clicking "add".
And then there will be a CheckoutView.
Check https://djangopackages.org/ (put "cart" in the search box). this will throw up several shopping cart things which might be the code you want, or the source of which might be a valuable learning resource before you end up rolling your own.
I need to Scrape Landing page and some pages that originate from this Landing page. And save all data into the same item. The originated pages do not connect between themselfs. What is the available way to do so? Is there any way that i can get(load) yielded item and add some info to it?
What I can think of is initiating a dict in spider itself but that seems very bad idea.
Here is a schema for better understanding. Sorry for my painting skills))
Say if i create item on page 1 (google) and send it as meta to all other links. Will Yielding the item from all 4 links come together and generate full item with info from 5 urls?
Ok. So I came up with two solutions.
Solution #1. As posted in original post send item to all the links via meta and add dictionary value to the same item field. Say item['links_info']={}. So every page will have its own key and data assigned to this item dict.
Solution #2. Save all links that need to be visited into a list. Follow 1 url from this list at a time and pass the rest via meta. Also pass item along.
The first one looks easier to implement though.
I have a long form that I want to break into multiple pages.
I am evaluating between two options for presentation:
Present the form on multiple pages using the FormWizard
Present the form on a slider like CSS3 slider.
The slider is actually just one page long using CSS3 to give impression of slides. It floats all of the content areas next to each other, hides the overflow, sets page width to say 500% if we have 5 slides, and moves the left-margin -100% to show the next slide. So it is all one page but seems like the form is being shown on sliding pages.
To me the advantage of slider approach is that there is only one form and the user submits the form only once at the end of slides and thus can go back and forth to make changes.(This is will be a common case).
Versus having mini-Forms for FormWizard and submitting them after each page. To me, FormWizards seems complicated especially if the user wants to change any of previous page responses. I also need FileField on my form pages and it seems like FormWizard accepts FileField only on the last page.
However, I have not seen many folks use this sliding forms (CSS3 or JScript one) approach. Hence, as a newbie, I am wondering if there are some obvious pitfalls of doing this?
I can only write about FormWizard as I have used it before. According to 'Handling Files' section of Django documentation which can be found in
https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/
a FileField can be used in any step of your FormWizard implementation. What I found really helpful by using FormWizard from Django, is that it provides you all the standard functionality of Forms. That means that you can easily create and manipulate forms, validate fields in each step, etc. I am not aware of how CSS3 slider accomplishes validation, but I guess that validation should take place on client side.
Additionally, Django's FormWizard uses either a Cookie storage backend, or a Session storage backend in order to store information, which makes the navigation between steps and modifying previous stored data pretty trivial.
I hope I helped you in your decision! A lot more information can be found of course in the Django documentation of FormWizard in the link I provided above.
i am working with a django quiz application where one question per page. when i will answer one question selecting a radio button and click submit button how can i get the next question in the next page as well as the answer will submitted to database. If anyone help me it would be an outstanding solution for me. please
Thanks for the answer , would you please i need bit specific answer. I wrote a view to get the question and render the answer in the radio link . but when i submit the answer how can i map the url to get dynamically new question each. thank you so much.
You need to write a view to handle your request. You need to edit urls.py to map your quiz url to the function in views.py (for your quiz app). So when a request with that quiz url comes Django applies that view function. So basically what this view does is this -
Get the answer & store it to DB. The DB details you need to define in models.py
Get the next question from DB (or wherever you store your questions).
Have a template (for quiz questions) with place holders for quiz question.
Form the new url & fill your template.
Redirect the user to the new url.
That does it. If you need any specifics lemme know.
I want to combine pagination with filtering. Since I have a lot of filters I do not want to send them per GET request, since the URLs get really ugly.
Since django pagination uses GET request to pass the page parameters, I do not know how I can combine these two approaches.
Any idea?
Great add-on would be: How can I combine this approach with table sort? :-)
Edit:
Actually it should work like the pagination of stackoverflow - user questions. If a user clicks on a page number one is shown the correct page, without showing the get parameters in the url.
This is the url called.
https://stackoverflow.com/api/userquestions.html?page=2&pagesize=10&userId=237690&sort=Recent
But the url shown in the browser is neat and short.
Seems to be ajax. Anybody an idea how to implement this? :)
If the URL is not shown in the browser`s address bar, I do not care about whether it is beautiful or not.
Edit: The solution:
Make an ajax update with all filter parameters passed to the view. This should help you get started with implementing ajax for your site: link
Thus the GET parameters never show up in the address bar.
have you checked the paginate application for django?
it may help you a lot, use it all the time :D
http://code.google.com/p/django-pagination/
Have you considered django-tables2? It gives you django-admin style tables without you having to write the logic yourself.
maybe you can use the urs, something like:
http://oursite.com/something/filter1/filter2/3/
the doc -> http://docs.djangoproject.com/en/1.1/topics/http/urls/
I figured out two solutions:
Instead of using just hyperlinks use it inside a POST form, i dont have any example now but i remember have used that for REST functions in Ruby on rails
Save the query info in a session.
Hope this help.