KEMBAR78
Django Instructions | PDF | Databases | Information Technology
0% found this document useful (0 votes)
12 views4 pages

Django Instructions

This document provides a step-by-step guide to setting up a Django project named 'll_project' with an app called 'learning_logs'. It includes instructions for creating models, views, templates, and URL configurations to manage topics and entries related to learning. The document also covers database migrations and the creation of an admin interface for managing the data.

Uploaded by

Arbana Kadriu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Django Instructions

This document provides a step-by-step guide to setting up a Django project named 'll_project' with an app called 'learning_logs'. It includes instructions for creating models, views, templates, and URL configurations to manage topics and entries related to learning. The document also covers database migrations and the creation of an admin interface for managing the data.

Uploaded by

Arbana Kadriu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

pip install django


2. django-admin startproject ll_project .
3. python manage.py migrate (create the database)
4. python manage.py runserver (localhost:8000)
5. python manage.py startapp learning_logs
6. Open setting.py->add “'learning_logs',” in INSTALLED_APPS (before default apps)
7. Open models.py-> create class called Topic:
class Topic(models.Model):
"""A topic the user is learning about."""
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""Return a string representation of the model."""
return self.text
8. python manage.py makemigrations learning_logs (modify database - The command
makemigrations tells Django to figure out how to modify the database so it can store the data
associated with any new models we’ve defined. The output here shows that Django has created
a migration file called 0001_initial.py. This migration will create a table for the model Topic in the
database. Now we’ll apply this migration and have Django modify the database for us:)
9. python manage.py migrate
10. python manage.py createsuperuser
11. Open admin.py file

from .models import Topic

admin.site.register(Topic)

12. Add a new model Entry (models.py):


class Entry(models.Model):
"""Something specific learned about a topic."""
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)

class Meta:
verbose_name_plural = 'entries'

def __str__(self):
"""Return a simple string representing the entry."""
return f"{self.text[:50]}..."
13. python manage.py makemigrations learning_logs
14. python manage.py migrate
15. Open admin.py file

from .models import Topic, Entry

admin.site.register(Topic)
admin.site.register(Entry)

16. Add some Topics/Entries: http://127.0.0.1:8000/admin/


17. Open urls.py:

from django.urls import path, include


urlpatterns = [
path('admin/', admin.site.urls),
path('', include('learning_logs.urls')),
]

18. Create a urls.py file in learning_logs folder:

"""Defines URL patterns for learning_logs."""


from django.urls import path, include
from . import views
app_name = 'learning_logs'
urlpatterns = [
# Home page
path('', views.index, name='index'),
]
19. Open views.py:

def index(request):
"""The home page for Learning Log."""
return render(request, 'learning_logs/index.html')
20. Inside the learning_logs folder, make a new folder called templates, and inside it a folder
learning_logs
21. Create here a file index.html:
<p>Learning Log</p>
<p>Learning Log helps you keep track of your learning, for any topic you're interested in.</p>
22. Refresh server
23. Create here a file base.html (parent template to use in inheritance):
<p>
<a href="{% url 'learning_logs:index' %}">Learning Log</a>
</p>
{% block content %}{% endblock content %}
24. Rewrite index.html to inherit from base.html:
{% extends 'learning_logs/base.html' %}
{% block content %}
<p>Learning Log helps you keep track of your learning, for any topic you're interested in.</p>
{% endblock content %}
25. Add a new Topics page, open learning_logs/urls.py, add:
# Page that shows all topics.
path('topics/', views.topics, name='topics'),
26. In views.py:
from .models import Topic
def topics(request):
"""Show all topics."""
topics = Topic.objects.order_by('date_added')
context = {'topics': topics}
return render(request, 'learning_logs/topics.html', context)
27. Make a file called topics.html:
{% extends 'learning_logs/base.html' %}
{% block content %}
<p>Topics</p>
<ul>
{% for topic in topics %}
<li>
<a href="{% url 'learning_logs:topic' topic.id %}">
{{ topic.text }}</a>
</li>
{% empty %}
<li>No topics have been added yet.</li>
{% endfor %}
</ul>
{% endblock content %}
28. Add the following code to base.html:
<a href="{% url 'learning_logs:topics' %}">Topics</a>
29. Add a new Topic page (for individual topics), open learning_logs/urls.py, add:
# Detail page for a single topic.
path('topics/<int:topic_id>/', views.topic, name='topic'),
30. In views.py add:
def topic(request, topic_id):
"""Show a single topic and all its entries."""
topic = Topic.objects.get(id=topic_id)
entries = topic.entry_set.order_by('-date_added')
context = {'topic': topic, 'entries': entries}
return render(request, 'learning_logs/topic.html', context)
31. Make a file called topic.html:
{% extends 'learning_logs/base.html' %}
{% block content %}
<p>Topic: {{ topic.text }}</p>
<p>Entries:</p>
<ul>
{% for entry in entries %}
<li>
<p>{{ entry.date_added|date:'M d, Y H:i' }}</p>
<p>{{ entry.text|linebreaks }}</p>
</li>
{% empty %}
<li>There are no entries for this topic yet.</li>
{% endfor %}
</ul>
{% endblock content %}

You might also like