How to use django-sitetree? - django

I am trying to use django-sitetree but I don't understand how to do step 3 which is:
"Go to Django Admin site and add some trees and tree items."
How to make a sitetree in the admin panel? I believe the first step is to choose an alias for the "Site Tree" you are about to add.
The next step is to add "Site Tree Item". On this page you have to choose parent, title, url. Considering my app is dynamic with url structure like this localhost:8000/categoryname/entryname how do I choose urls?
By the way I am trying to add breadcrumbs in my templates.

To create a tree:
Goto site administration panel;
Click +Add near 'Site Trees';
Enter alias for your sitetree, e.g. 'maintree'.
You'll address your tree by this alias in template tags;
Push 'Add Site Tree Item';
Create first item:
Parent: As it is root item that would have no parent.
Title: Let it be 'My site'.
URL: This URL is static, so put here '/'.
Create second item (that one would handle 'categoryname' from your 'categoryname/entryname'):
Parent: Choose 'My site' item from step 5.
Title: Put here 'Category #{{ category.id }}'.
URL: Put named URL 'category-detailed category.name'.
In 'Additional settings': check 'URL as Pattern' checkbox.
Create third item (that one would handle 'entryname' from your 'categoryname/entryname'):
Parent: Choose 'Category #{{ category.id }}' item from step 6.
Title: Put here 'Entry #{{ entry.id }}'.
URL: Put named URL 'entry-detailed category.name entry.name'.
In 'Additional settings': check 'URL as Pattern' checkbox.
Put '{% load sitetree %}' into yor template to have access to sitetree tags.
Put '{% sitetree_menu from "maintree" %}' into your template to render menu.
Put '{% sitetree_breadcrumbs from "maintree" %}' into your template to render breadcrumbs.
Steps 6 and 7 need some clarifications:
In titles we use Django template variables, which would be resolved just like they do in your templates.
E.g.: You made your view for 'categoryname' (let's call it 'detailed_category') to pass category object into template
as 'category' variable. Suppose that category object has 'id' property.
In your template you use '{{ category.id }}' to render id. And we do just
the same for site tree item in step 6.
In URLs we use Django's named URL patterns (documentation). That is almost idential to usage of Django 'url' tag in templates.
Your urls configuration for steps 6, 7 supposed to include:
url(r'^(?P<category_name>\S+)/(?P<entry_name>\S+)/$', 'detailed_entry', name='entry-detailed'),
url(r'^(?P<category_name>\S+)/$', 'detailed_category', name='category-detailed'),
So, putting 'entry-detailed category.name entry.name' in step 7 into URL field we tell sitetree to associate that sitetree item with URL named 'entry-detailed', passing to it category_name and entry_name parameters.
I hope this description should fill the documentation gap %)

Related

How to set custom/editable site name and discription Django CMS

How to set a custom/editable sitename and site discription in Django CMS.
The sitename and discription should be editable via Django CMS!
Check the image
I'm not entirely sure what you mean, but if I understand you correctly, you want to
put a placeholder in your template and
add a text plugin to that via the structure view.
<!-- your template, e.g. base.html -->
{% placeholder "title" %}
Then, in your structure view (button at the top right in your screenshot) you should see the placeholder and be able to add a text plugin with the title there.

Django redirect to form page and submit the form?

Right now. I have a search function in my page to search for item id. When I click search, I will render the same page with the result items and show item. And in other pages where I also display the item id, I want to add a link to the id to go to the same page where I search for that id.
Example: id: 123, I want the same page when:
1. search '123' in my search page(my search only accept exact match)
2. In other pages, click '123', go to the search page with results
How should I achieve this, I have tried many ways which don't wok.
You need to make use of the GET method that HTML forms provide. When you perform a search from the first page, you must make sure that you are doing so using the GET method in the form. This will append the form data into the URL.
E.g. If you have a 'name' field in your form which has 'John' inputted. The submission of this form will compose a URL like so:
http://someurl.com/?name=John
This can then be accessed using the Django request object:
name = request.GET['name']
You've probably done something similar already for displaying your search results. So, all you need to do is create a link in your second page that redirects to the search page with GET request variables appended.
E.g.
<a href="{% url 'search_page' %}?searchterm=232> Item 232 </a>

Django: How to make this GET request with a hyperlink?

So if I want a user's name to link to it's profile, I want the link to contain the user's first name and last name in GET form...Do I just hard-code this into the link, or is there a more elegant way of coding this? Here is hard-coded:
<p>
<li role="presentation" class="active">{{ user.fname }}
</li>
</p>
Yes, the more elegant way would be to set up a route that takes, for example, the user's ID (pk) and use either a DetailView set up for the User model or a function view that accepts the ID and retrieves the relevant user. Passing in the first and last name directly means you need to query on both of them (after pulling them out of the querystring) rather than simply querying on the ID as it's passed to you (after the framework politely checks its type, provided you've set up the route correctly).
So your route would look something like url(r'^profile/(?P<pk>\d+)/$', ProfileView.as_view(), name='profile') and your template tag would look something like {% url 'profile' user.id %}.

Django advanced nesting urls

Welcome,
I've got a problem where I`m trying to make a deep nesting.
The thing is that I have Menu that has SUBMENU that can have multiple categories and those categories can have multiple services available and those services can have multiple products.
Writing urls for that specific thing was easy but when I'm trying to create a product that has no category and I want it to be displayed directly in SUBMENU the problem is with writing url for that specific product that doesnt overlap with SUBMENU slug.
For example:
MENU > SUBMENU1 > CATEGORY 1 > AVAILABLE SERVICES > PRODUCT 1
MENU > SUBMENU2
MENU > SUBMENU3 > PRODUCT 2
all of those names are slugged and my urls.py looks like this:
url(r'^uslugi/(?P<category_slug>\S+)/(?P<services_slug>\S+)/(?P<service>\S+)/$', views.show_service_details, name='show_service_details'),
url(r'^uslugi/(?P<category_slug>\S+)/(?P<services_slug>\S+)/$', views.show_services, name='show_services'),
url(r'^uslugi/(?P<category_slug>\S+)/$', views.show_categories, name='show_categories'),
url(r'^uslugi/(?P<product_slug>\S+)/$', views.show_product_without_cat, name='show_product_without_cat'),
The thing is that when I try to enter a product with no category (just placed in submenu), my urls are calling the show_category view. Changing sequence of those urls won't resolve my problem, because I won't be able to enter my show_categories because django will try to execute show_product_without_cat
I there a reasonable solution for this without redesigning all structure ?
Unfortunetely even saying to django in template to call specific VIEW doesn't help at all. It just goes through all urls and matches the first one
<a href="{% url 'show_product_without_cat' i.url %}" >
<img src="/media/{{ i.image }}"> <br/>
</a>
URL dispatcher finds the first url that matches the request path and calls the found view. You have the same url regex for both show_categories and show_product_without_cat views so only the first occurrence of url will work.
The only solution is to create an intermediate view which will check the slug against Category or Product model and call the appropriate view.
def product_or_category(request, slug):
if Product.objects.filter(slug=slug).exists():
return show_product_without_cat(request, slug)
return show_categories(request, slug)
And assign the url to this view:
url(r'^uslugi/(?P<slug>\S+)/$', views.product_or_category,
name='show_product_or_category'),
But note that with this solution you can't have a category and product with the same slug.

Unexpected URL concatenation

I have a url for product categories
url(r'^(?P<cat>[^/]+/$)', 'reviews.views.category'),
for example, if I am in the url: http://127.0.0.1:8000/ and click on "eletronics" in the menu, I go to http://127.0.0.1:8000/eletronics/
If I click again in other category (e.g. "books"), I go to http://127.0.0.1:8000/eletronics/books/
How I set up my url in way that I just get one (the last clicked) category in the url?
The menu links in your template may be pointing to "electronics/" and "books/" vs "/electronics" and "/books." To conform with DRY, I recommend naming your url patterns and using the {% url %} template tag to load the correct url route.
For example:
url(r'^(?P<cat>[^/]+/$)', 'reviews.views.category', name='category'),
And in your template:
{% url 'category' 'books' %}
Which will return the correct '/books' url route.