This is my first project with Django, I want to recreate my static HTML/CSS/js site to a dynamic site with an admin panel. However, I have a hard time understanding the views/urls.On my index, I have main news, events, mini news - 3 categories. I can render mainnews, however, I'm not sure what to use as 'return' for the other 2(all 3 are on the belongs to index page) Currently, I have 'index' but doesn't show the events/news.
pages>view.py
from django.shortcuts import render, redirect, get_object_or_404
from mainnews.models import Mainnews
from events.models import Event
from mininews.models import Mini
# Create your views here.
def home_view(request):
main_news = Mainnews.objects.order_by('-publish_date').filter(is_published = True)[:1]
context = {
'main_news' : main_news
}
return render(request, 'index.html', context)
def event_view(request):
events = Event.objects.order_by('-publish_date').filter(is_published = True)[:3]
context = {
'events' : events
}
return render(request, 'index.html', context)
def mini_view(request):
mini_news = Mini.objects.order_by('-publish_date').filter(is_published = True)[:4]
context = {
'mini_news' : mini_news
}
return render(request, 'index.html', context)
main>urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from pages.views import home_view, event_view, mini_view
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name = 'home'),
path('', event_view, name = 'home'),
path('', mini_view, name = 'home'),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
this is how it looks the html
{% block content %}
<!-- Section 2 News and Events -->
<div id="news-container">
<div class="jumbo-news">
{%if main_news%}
{% for obj in main_news%}
<img id = 'jumboImgUrl' class='jumbo-img'
src="{{ obj.image.url }}">
<h2 id = 'jumboTitle' class='jumbo-title'>{{ obj.title }}</h2>
<h4 id = 'jumboDescription' class='jumbo-parag'>{{ obj.description }}</h4>
{% endfor %}
{% else %}
<div class = 'col-md-12'>
<p>No news available</p>
</div>
{% endif %}
</div>
<div id="events" class='eventBox'>
<h3 class='events-title'>Events <i class="far fa-calendar-alt"></i></h3>
<div id = 'allEvents' class='all-events'>
{% if events%}
{% for event in events %}
<div id='event' class='event'>
<div class='event-date'>
<h4 class='date-helper'>{{ event.days }}</h4>
<div class='event-line'></div>
<h4 class = 'date-month'>{{ event.month }}</h4>
</div>
<div class = 'paragrah'><p id = 'eventDesc' class='event-text'>{{ event.description}}</p></div>
</div>
{% endfor %}
{% else %}
<div>
<p>No events available</p>
</div>
{% endif %}
</div>
</div>
</div>
</div>
</div>
{% endblock %}