Reputation: 239
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 %}
Upvotes: 0
Views: 44
Reputation: 96
ok, in django url must be conncted to only one function or class (doesnt matter) in your views.py. but you are connecting 3 different path(consider one although with same names) to 3 different path.imagine this if i set a request for your site for you home page sth like www.yoursite.come/
how do you want to separate those three and how to choice to use which one.but you can set query as much as you want to as many models you have in one single function like this :
def home_view(request):
main_news = Mainnews.objects.order_by('publish_date').filter(is_published = True)[:1]
events = Event.objects.all().order_by('-publish_date').filter(is_published = True)[:3]
mini_news = Mini.objects.order_by('-publish_date').filter(is_published = True)[:4]
context = {
'main_news' : main_news,
'events' : events,
'mini_news': mini_news
}
return render(request, 'index.html', context)
and in your url
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name = 'home'),
and then in your template show it all. and if you want to show it separately you can keep you code and just change the url to this
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name = 'home'),
path('event_view/', event_view, name = 'event_view'),
path('mini_view', mini_view, name = 'mini_view'),
]
Upvotes: 1
Reputation: 239
def home_view(request):
main_news = Mainnews.objects.order_by('-publish_date').filter(is_published = True)[:1]
events = Event.objects.all().order_by('-publish_date').filter(is_published = True)[:3]
mini_news = Mini.objects.order_by('-publish_date').filter(is_published = True)[:4]
context = {
'main_news' : main_news,
'events' : events,
'mini_news': mini_news
}
return render(request, 'index.html', context)
Upvotes: 0