Introduction
The project's goal is to build a clone of a simple social network examining the architecture and use of Django. It is built incrementally, every week, with a group of students during Coding Summer 23 activities.
We use Visual Studio Code as a tool, but PyCharm or other IDEs can be used instead.
The database of the system is SQLite for simplicity. In a later stage, we may consider trying to migrate to a different DBMS.
Create the Project
Virtual environment: In a folder (cs23_twitter) we create the virtual environment:
python -m venv venv
. Then we activate the virtual environment by:source venv/bin/activate
(for Mac/Linux) orvenv\Scripts\activate
(for Windows).Install Django:
pip install Django
Create the project:
django-admin startproject twitter_clone
, then cd into the project's directory:cd twitter_clone
.Migrate: Django comes with preinstalled applications that cover authentication, admin, etc. Initial migration will create all the necessary tables in the database.
pyhon manage.py migrate
.Run the server:
python manage.py runserver
. By default, server is running on port 8000, so if we type: http://127.0.0.1:8000, the Django success page appears:In the projects folder we can find several files. Explore urls.py used for settings the project's URLs, associating them to controllers, and settings.py used for configurations.
Admin page: Django includes an administration page at the address: 127.0.0.1:8000/admin. We can login to the page using the superuser credentials we just created. The page looks like this:
Create an Application
This is a little confusing. Don't we already have an application?
Well, we have a project. A Django application is a package inside the project. A project may contain many applications, especially if we want to create reusable components.
Create the application:
python manage.py startapp tweets
. This will create a new folder, tweets, and in the folder we can find files like views.py (in fact the controller), models.py, admin.py, etc, and a migrations folder.Add to settings: In the settings.py file find the
INSTALLED_APPS
list, and add the application to it as'tweets'
.
Design the Database
This is a very simple and maybe not correct initial database design, we will adjust as we go. Only the primary keys are displayed. The purpose is to get an idea of the models and their relationships.
First Model and feed page
Now it's time to create our first model, migrate to the database, add data (tweets) and display the data on a respective web page.
Create the model: In the models.py file we create a new class Tweet, representing the tweets entity:
class Tweet(models.Model): content = models.CharField(max_length=140) published_on = models.DateTimeField(default=datetime.now()) user = models.ForeignKey(User, on_delete=models.CASCADE)
Create migration:
python manage.py makemigrations
.Run migration:
python manage.py migrate
.Register model to admin: In the admin.py file, add an entry:
admin.site.register(Tweet)
. This will also require importing the Tweet class:from tweets.models import Tweet
.Populate the database: Now the new table is accessible via the admin page. We can add, modify, or delete users and tweets.
Create view: In the views.py file we create our first view as a function (later we will use classes, too). This function retrieves all tweets from the database, using the Tweet model, and then passes the tweets to the template feed.html in a dictionary:
def feed(request): all_tweets = Tweet.objects.all() return render(request, "feed.html", {'tweets': all_tweets})
Add URL: In the urls.py file we add an entry to the
urlpatterns
list:
path('feed', tweets.views.feed),
This associates the URL /feed with the controller's function. We also need to import tweetsCreate the web page: First we create a folder called
templates
in the project's main folder. We update TEMPLATES entry in the settings.py file to define the location of the templates:'DIRS': [BASE_DIR / 'templates'],
.
Then, in the templates folder we create the feed.html page where we add a loop using the{% %}
notation and display the tweets using the{{ }}
notation:<body> <h1>Twitter Clone Feed</h1> {% for tweet in tweets %} <p> <b>{{ tweet.user.username }}</b>: {{ tweet.content }} </p> {% endfor %} </body>
Now we can check our page at
http://127.0.0.1:8000/feed
.
Summary and next steps
We set up a new Django project and created our first Model, View and Template. The code for this version is available at:
https://github.com/CodingSummer23/cs23_twitter, branch week-1
In the next step we will add the ability to login and post new tweets.