Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It’s a versatile tool that can be used for a wide range of web development projects, from simple blog sites to complex web applications.
In this tutorial, we’ll be exploring how to create a website for a fictional movie trailer service called "Zwiastun PL" using Django. We’ll cover how to set up a Django project, create models, views, templates, and how to integrate static files and media files into our project.
Step 1: Setting up a Django project
First, make sure you have Django installed on your machine. You can do this by running the following command in your terminal:
pip install django
Next, create a new Django project by running the following command in your terminal:
django-admin startproject zwiastunpl
cd zwiastunpl
This will create a new directory called "zwiastunpl" with a basic Django project structure.
Step 2: Creating models
In our movie trailer service, we’ll have two models: Movie
and Trailer
. In your models.py
file, define the models as follows:
from django.db import models
class Movie(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
release_date = models.DateField()
def __str__(self):
return self.title
class Trailer(models.Model):
movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
video_url = models.URLField()
def __str__(self):
return self.movie.title
After defining the models, run the following command in your terminal to create the database tables:
python manage.py makemigrations
python manage.py migrate
Step 3: Creating views
Next, we’ll create views to display our movie trailers. In your views.py
file, define the views as follows:
from django.shortcuts import render
from .models import Movie, Trailer
def movie_list(request):
movies = Movie.objects.all()
return render(request, 'movie_list.html', {'movies': movies})
def movie_detail(request, movie_id):
movie = Movie.objects.get(pk=movie_id)
trailers = Trailer.objects.filter(movie=movie)
return render(request, 'movie_detail.html', {'movie': movie, 'trailers': trailers})
Step 4: Creating templates
Now, create two HTML templates for displaying the movie list and movie details. In your templates
directory, create a new directory called zwiastunpl
and create the following templates:
movie_list.html
:
<!DOCTYPE html>
<html>
<head>
<title>Zwiastun PL</title>
</head>
<body>
<h1>Movies</h1>
<ul>
{% for movie in movies %}
<li><a href="{% url 'movie_detail' movie.id %}">{{ movie.title }}</a></li>
{% endfor %}
</ul>
</body>
</html>
movie_detail.html
:
<!DOCTYPE html>
<html>
<head>
<title>{{ movie.title }}</title>
</head>
<body>
<h1>{{ movie.title }}</h1>
<p>{{ movie.description }}</p>
<h2>Trailers</h2>
<ul>
{% for trailer in trailers %}
<li><a href="{{ trailer.video_url }}">{{ trailer.movie.title }}</a></li>
{% endfor %}
</ul>
</body>
</html>
Step 5: Setting up URLs
Finally, set up the URLs for your views in your urls.py
file:
from django.urls import path
from . import views
urlpatterns = [
path('', views.movie_list, name='movie_list'),
path('movie/<int:movie_id>/', views.movie_detail, name='movie_detail'),
]
Step 6: Running the server
You’re now ready to run your Django server! Start the server by running the following command in your terminal:
python manage.py runserver
Navigate to http://127.0.0.1:8000/
in your web browser to see your movie trailer service in action.
Congratulations, you’ve successfully created a movie trailer service using Django! Feel free to customize the project further by adding more features such as user authentication, search functionality, or custom styling. Django offers a robust set of tools that make it easy to expand and enhance your web applications. Happy coding!
😆
Najlepszy film jaki kiedykolwiek widziałem!
"Django" to film, który zachwyca pod każdym względem. Jest on na 3 miejscu w moim rankingu ulubionych filmów. Moim zdaniem "Django" to najlepszy film Tarantino. ARCYDZIEŁO!!! Polecam go każdemu, nawet tym, co nie przepadają za westernami, gdyż mogę z czystym sumieniem stwierdzić, że dostarczy on niezapomnianych emocji i dobrej zabawy każdemu.
Moja ocena: 10/10.
Co tu dużo mówić, kolejna perełka Tarantina
Był.Ostatnio sobie grałem w tą grę na ps3 i natrafilłem tam na murzyna (podczas strzelaniny),w dodatku policjanta.
Johny Cash <3
ja myślę że tanimi chwytami nabierają nas na filmach transformers czy szybcy i wściekli a nie tarantino
Tarantino nie ma niczego w dupie, kręci komercyjną sieczkę pod publiczkę, i nabiera niewyrobionego widza tanimi chwytami.
Jego filmy trzeba lubić bo sa inne niż ten cały hollywoodzki chłam który ty uważasz za super kino. Tarantino robi to co chce i ma w dupie co o tym sądzą inni i to mi się w nim podoba
Django, "D" się nie wymawia – najlepsze
Django jest b. dobry. Pulp Fiction też. Niemniej – reszta to taki twór disco.
Kubrick był geniuszem, a Tarantino to taki Pikej, tyle że zamiast śpiewać kręci filmy. Udaje mu się nabierać miłośników multipleksowego kina na swoją tandetę.
Tarantino jest parodią samego siebie, tani zachowawczy politycznie poprawny, razem z Woody Allenem tworzą duet największych blagierów kina światowego. Obaj kreują się na niepokornych, odkrywczych prześmiewców a w rzeczywistości płyną z prądem mody jak nikt inny. Nabierają na swoją twórczość tylko nastolatków miłośników horrorów o wampirach, którzy myślą że obcują z wielkim i błyskotliwym kinem.
duzo jest bledów w tlumaczeniu ;). Koleś mówił ze przeszukają kazdą plantację póki nie znajdą ICH, a przetlumaczono, póki nie znajdą JĄ ;).
Sceny wyrwane z kontekstu, stąd tez pewno wiele blędow.
Tarantino jest geniuszem.
Ale Kubrick był większym.
lol od kiedy "do you know what the bounty hunter is" oznacza znasz się na dynamitach? chyba nie jednak
CZARNY humor ? 😉
Dobra fabuła, dobre jajca, czarny humor, dobra akcja, dziki zachód, dobitna brutalność… eeeh nie chce mi się już wymieniać :-/
tym sie różni właśnie kino Tarantino 🙂
Jaki Django ma związek z RDR?