how to solve two views of the same url (Home) - Django

0

How is it going?
I write because I do not know how to solve the following question in Django (from my condition again in this):
I have two apps that must be shown in the same template.
That is, they share the url that is the home, and they have two different views.
I share code.

The help is appreciated

Django==2.0.2 <br>
Python 3.6.5 :: Anaconda, Inc.

views.py

from django.shortcuts import render
from .models import Post

# Create your views here.

def blog(request):
    posts = Post.objects.all()
    return render(request, 'about/home.html', {'posts': posts})

views.py

from django.shortcuts import render
from .models import Project

# Create your views here.
def portfolio(request):
    projects = Project.objects.all()
    return render(request, "about/home.html", {'projects':projects})

urls.py

from django.urls import path
from django.contrib import admin
from about import views
from portfolio import views as portfolio_views

from django.conf import settings


urlpatterns = [

    path('admin/', admin.site.urls),
    path('', views.blog, name = "home"),
    path('', portfolio_views.portfolio, name="portfolio"),

]

if settings.DEBUG:
    from django.conf.urls.static import static
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Part of the home.html

<section class="bg-primary" id="about">
  <div class="container">
    <div class="row">
      <div class="col-lg-8 mx-auto text-center">
        {% block background %}
          {% for post in posts %}
            <h2 class="section-heading text-white"> {{post.title}} </h2>
            <hr class="light my-4">
            <p class="text-faded mb-4"> {{post.content}} </p>
          {% endfor%}
        {% endblock%}
        <a class="btn btn-light btn-xl js-scroll-trigger" href="#services">iniciemos el viaje</a>
      </div>
    </div>
  </div>
</section>

    <section class="p-0" id="portfolio">
  <div class="container-fluid p-0">
    <div class="row no-gutters popup-gallery">
      {% block content %}
        {% for project in projects %}
          <div class="col-lg-4 col-sm-6">
            {% if project.link %}
            <a class="portfolio-box" href="{{project.link}}">
            {% endif%}
              <img class="img-fluid" src="{{project.image.url}}" alt="">
              <div class="portfolio-box-caption">
                <div class="portfolio-box-caption-content">
                  <div class="project-name">
                      {{project.title}}
                  </div>
                </div>
              </div>
            </a>
          </div>
        {% endfor%}  
      {% endblock%}  
    </div>
  </div>
</section>
    
asked by Seba 04.11.2018 в 17:48
source

1 answer

1

2 views can not share the same URL. In your code, Django will end up solving for the first one you find, in which case it is views.blog . To achieve both functionalities in a single screen in a simple way you must combine your logic in a single view and redefine the file urls.py towards it. For example:

def new_view(request): 
    django.shortcuts import render
    from <app1>.models import Project
    from <app2>.models import Post

    posts = Post.objects.all() 
    projects = Project.objects.all()

    return render(request, 'about/home.html', {'posts': posts,' projects':projects})
    
answered by 05.11.2018 в 01:54