News App - ( Web Development with Django )
Heading h2
Syntax
# urls.py
from django.urls import path
from . import views
app_name = 'news'
urlpatterns = [
path('', views.index, name='index'),
path('article/<int:article_id>/', views.article, name='article'),
path('category/<category>/', views.category, name='category'),
]
# views.py
from django.shortcuts import render, get_object_or_404
from .models import Article, Category
def index(request):
articles = Article.objects.all().order_by('-date')
return render(request, 'news/index.html', {'articles': articles})
def article(request, article_id):
article = get_object_or_404(Article, pk=article_id)
return render(request, 'news/article.html', {'article': article})
def category(request, category):
articles = Article.objects.filter(category__name=category).order_by('-date')
return render(request, 'news/category.html', {'category': category, 'articles': articles})
Example
Result
A fully functional news website built with Django, that allows you to:
- View the latest articles on the homepage
- View individual articles
- Browse articles by category
Output
Homepage
Article
Category
Explanation
The News App is a web application built using Django, a web framework for Python. It provides a platform for publishing articles and categorizing them based on user-defined categories.
The web application has three main features:
- Index page to display articles
- View individual articles
- Browse articles by category
The website is built using Django's model-view-template (MVT) architecture, which separates the application logic into three distinct components: the model, the view, and the template.
The model is responsible for the data and interacts with the database to store and retrieve article data. The view handles the application logic and renders the appropriate template for each request. The template is responsible for rendering the HTML content that is displayed to the user.
Use
The News App can be used as a starting point for building your own news website. It provides a solid foundation for publishing articles and categorizing them.
You can customize the website to your own needs by changing the HTML templates, adding new views or modifying existing ones. You can also extend the functionality of the website by adding new models or modifying existing ones.
Important Points
- The News App is built using Django, a web framework for Python
- The website provides a platform for publishing articles and categorizing them
- The MVT architecture separates the application logic into three distinct components: the model, the view, and the template
- The News App can be customized to your own needs by changing HTML templates, adding new views, or modifying existing ones
Summary
In conclusion, the News App is a fully functional news website built using Django. It provides a solid foundation for publishing articles and categorizing them. The website is built using the MVT architecture, which separates the application logic into three distinct components: the model, the view, and the template. The News App can be customized to your own needs by changing HTML templates, adding new views, or modifying existing ones.