I'm a beginner in django-oscar and I try to manage a new view on the page.
I've already created two pages with django-oscar dashboard,
https://ibb.co/cM9r0v
and made new buttons in the templates:
Lib/site-packages/oscar/templates/oscar/partials/nav_primary.html
https://gist.github.com/Kalinar/076fc8144869c3b50fc0bc9e52f825e4
I have no idea how to make a good a href="???" to new pages in buttons ... can someone help?
Maybe there is better way to do it, can you explain it to me?
Oscar uses Django's flatpages app, so you can use their template tags to dynamically add in navigation links to the pages you create.
{% load flatpages %}
{% get_flatpages as flatpages %}
{% for page in flatpages %}
<li class="dropdown active">
<a href="{{ page.url }}" class="dropdown-toggle" {% if not expand_dropdown %} data-toggle="dropdown"{% endif %}>
{% trans page.title %}
</a>
</li>
{% endfor %}
You can find more information on the flatpages app in the Django flatpage documentation.
You already know the title and of course the id of the page, and each page is an instance of the model PagePromotion, just query the model for such name/id and use the attribute page_url of the returned instance to send it within a variable in the context of the view that renders the menu.
Then in the template:
href="{{ variable_containing_url }}"
Related
I am making my own portfolio website using Django.. So the idea is: Make posts containing websites that i've developed. All these posts will be displayed in a page called "Projects" and then you can access a single project to read about it. Every project i upload have an ID, so i used this code to acess each project page:
# Single project page
path('projects/<int:post_id>/', views.project, name='project'),
Then in my PROJECTS page i have a simple(for now) HTML code to show all my projects and link them to the single project page. What i want to know is how to LINK this ID generated URL in this code:
<ul>
{% for project in projects %}
<li>{{ project }}</li>
{% empty %}
<li>No project have been added yet.</li>
{% endfor %}
</ul>
I tried using:
<li><a href="{% url 'main_portfolio/projects/<int:post_id>/' %}</a></li
And some other things but nothing i try is working.
Maybe try <a href="{{ project.get_absolute_url }}">, or
<a href="projects/{{ project.id }}/">
You can work with the {% url … %} template tag [Django-doc]:
<li>some text</li>
I have an app in Django where users upload some files(xlsx), the information gets extracted and the files are stored in /media.
I want a page/view where the users should be able to browse through the files by folder structure or alphabetically and they should be able to download them from the server.
What i have now is as basic as it gets:
this is the .html
{% block Content%}
{% if documents %}
<ul id="files_ul">
{% for document in documents %}
<li>
{{ document.docfile.name }}
</li>
{% endfor %}
</ul>
{% else %}
<p> No documents. </p>
{% endif %}
{% endblock %}
I have a model named Document and i've added this:
+ static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
to urls.py which i know it's not secure.
I don't need something like Filer because i already have that but i didn't yet figure out how to configure.
I only know python and that not extensively and i'm new to Django so any help would be really appreciated.
It would be of great help if you could show me at least how to change the name of the files that appear now in this view,
document.docfile.name
Results in folder1/folder2/filename and i would want only the filename to appear.
Model
class Document(models.Model):
def filename(self):
return os.path.basename(self.file.name)
upload_user=models.ForeignKey(User, related_name='documents')
upload_time=models.DateTimeField(auto_now_add=True)
docfile=models.FileField(upload_to='documents/%Y/%m')
in your model where you have defined filefield put this function
def filename(self):
return os.path.basename(self.docfile.name)
and in template
{{ document.filename }}
I am using Django and developing an i18n site serving many languages. I want to make a modal that stays in base.html, so that users can switch the language wherever they are.
I managed to do something like this.
<div class="modal-body">
{% get_available_languages as languages %}
{% for lang_code, lang_name in languages %}
{% language lang_code %}
{{lang_code|lang_name}}
{% endlanguage %}
{% endfor %}
</div>
Which turns out urls like:/ja/, /en/, /fr/, etc..
but this kind of approach links to the main page only.
When using {{request.path}} or {{request.get_full_path}} for the url like:
{{lang_code|lang_name}}
It doesn't include the i18n url patterns..
Is there any way for directing current url with request.path??
TARGET
When in /foo/ : /ja/foo/ /en/foo/ /fr/foo/
When in /bar/ : /ja/bar/ /en/bar/ /fr/bar/
Thanks in advance!
This topic is discussed in this SO question: Django templates: Get current URL in another language.
In my project, I use this simple template tag (taken from https://djangosnippets.org/snippets/2875/), which returns the URL of the current view in another language.
foo/templatetags/i18n_urls.py:
from django import template
from django.urls import translate_url
register = template.Library()
#register.simple_tag(takes_context=True)
def change_lang(context, lang: str, *args, **kwargs):
path = context['request'].path
return translate_url(path, lang)
some_template.html:
{% load i18n_urls %}
<ul>
<li>
EN
</li>
<li>
CS
</li>
<li>
DE
</li>
</ul>
Please note that translate_url function is not documented in the official Django docs. Here is the source code of this function: https://github.com/django/django/blob/master/django/urls/base.py#L161-L181.
I have installed django-page-cms successfully i think. Like other cms, it is also for creating new pages. But I already have html pages in my project. How to integrate with that?
They want me to put place holder in html page, like:
{% load pages_tags %}
but I think this will bring the content from the already created page in admin
Can anyone tell me how to integrate with my existing pages?
First you need to create page in admin console. Then add the placeholder in your template
like what tutorial saying
{% get_page "news" as news_page %}
{% for new in news_page.get_children %}
<li>
{{ new.publication_date }}
{% show_content new body %}
{% endfor %}
I have three apps in my Django project that correspond to separate parts of the website. All three parts are accessible from the navigation menu, defined in common base.html template.
I want to be able to identify the app that called the template to add an 'active' css class to the menu option corresponding to the active part of the site.
What's the best way to do it, short of modifying each view to pass an additional variable to the template?
The most non-invasive way would be:
Write a template tag that generates your menu.
Update your context with the application name.
Pass this to the template tag and modify the css accordingly.
I'll explain the second bit, as writing custom template tags is covered in detail in the django documentation.
To update the context; we need some middleware. This should do what you need:
class SetApplicationName(object):
def process_view(self, request, view_func, view_args, view_kwargs):
request.current_app = view_func.__module__.split('.')[0]
Place this somewhere django can find it (in any directory in PYTHONPATH), and add it to your middleware classes. Also, make sure you have django.core.context_processors.request in your template context processors.
In your templates, now you have {{ request.current_app }} which should point the app name.
Your template tag should be something like {% navigation_menu request.current_app %}, and you can then modify your menu css accordingly.
I would try "overriding" the {%block%} tag.
In your base.html template put something like:
{%block navigation_bar%}
<div class="regular">First app</div>
<div class="regular">Second app</div>
<div class="regular">Third app</div>
{%endblock%}
In your descendant templates, change that navitation_bar block with other. first_app_base.html looks like:
{%extends "base.html"%}
{%block navigation_bar%}
<div class="active">First app</div>
<div class="regular">Second app</div>
<div class="regular">Third app</div>
{%endblock%}
If you define navigation menu in separate template, you could include it with additional context.
base.html:
{% block navigation %}Here will be navigation{% endblock %}
template_from_some_app.html:
{% extends "base.html" %}
{% block navigation %}
{% include "navigation.html" with active_app='second_app' %}
{% endblock %}
navigation.html:
<ul class="nav">
<li {% if active_app == 'first_app' %} class="active">{% endif %}>
First app
</li>
<li {% if active_app == 'second_app' %} class="active">{% endif %}>
Second app
</li>
<li {% if active_app == 'third_app' %} class="active">{% endif %}>
Third app
</li>
</ul>