Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Tech C**P
Django has an admin panel that does lots of tedious works that if you had to create yourself, it wouldn't be an easy t ask. In this part we try to build the admin panel from ground up. First of all an admin panel needs an admin user. So create one first:…
Django has an admin panel that does lots of tedious works that if you had to create yourself, it wouldn't be an easy task. In this part we try to build the admin panel from ground up.

First of all an admin panel needs an admin user. So create one first:
$ python manage.py createsuperuser
Username (leave blank to use 'fc'): alireza
Email address: a***@***.com
Password:
Password (again):
Superuser created successfully.


Ok after creating the admin user, run the developement server:
$ python manage.py runserver


Now point your URL to http://127.0.0.1:8000/admin/. You should see the login page in the next post. Login and see the administrator page of Django.

#python #django #admin #dashboard #createsuperuser #django_part6


Now to see polls in admin page, you need to register you models. open polls/admin.py:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

from .models import Question
admin.site.register(Question)


Last 2 lines are added to register the Question model as picture below:
Media is too big
VIEW IN TELEGRAM
Django interactive shell

#python #django #shell #django_part7
Media is too big
VIEW IN TELEGRAM
Routes implementation in `Django` and how to wire routes & views

#python #django #route #view #django_part8
One of the benefits of Django`'s `QuerySet is that you can apply filters on it, slice it and move it around your classes without actually hitting the database until you do so.

One of the ways that executes the query is using iteration:
for c in Course.objects.all():
print(c.title)
NOTE: the first time you iterate over the query, it hits the database and get the result NOT in each iteration.

In the below ways your query will be evaluated (executed):
- repr
- len
- list (e.g.: `list(Entry.objects.all())`)
- bool

#python #django #querySet #query_set #evaluation
DJANGO

Let's do a little bit more by fetching data from database and pass them to views in Django.

For now we make a change in polls/views.py in index function to get data from Question model and return it as an HTTP response object. At first let's import the model:
from .models import Question


And now in index function write the below code:
latest_question_list = Question.objects.order_by('-pub_date')[:5]
output = ', '.join([q.question_text for q in latest_question_list])
return HttpResponse(output)


The code above gets the last 5 questions and on the second line question_text fields for those que
stions are joined together by comma and finally returned as an HTTP response.

Now if you head over to http://127.0.0.1:8000/polls/ you would see the result of question texts bound together by co
mma.

#python #django #query #django_part9
Here we aim to design templates and render them to interact with Django the way it likes! We try to isolate the page's design.

Create a folder called templates in polls directory (this is the default folder name where django will look for app templates). Inside of templates create polls directory and inside of it create index.html file. So your templates are in path polls/templates/polls/index.html.

We have polls inside of templates folder to render templates in the format of loader.get_template('polls/index.html').

NOTE: if you don't create polls subdirectory inside of templates and you have templates we the same name in different apps (in the same project) you would encounter bizarre problems. DON'T DO THAT! (Django will choose the first template it finds whose name matches)

Now put the below code in polls/templates/polls/index.html:
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}


OK now do as follow in index function in polls/views.py for template rendering:
from django.http import HttpResponse
from django.template import loader
from .models import Question


def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = {
'latest_question_list': latest_question_list,
}
return HttpResponse(template.render(context, request))


Note that we have imported loader from django.template.

As you can see above, we have rendered the template with our variables (context) and passed it to http response object. We have used django.template module to render templates.

I've got the below result (next picture) what about you?

#django #loader #render #templates #django_part10
You can integrate Django with Bootstrap4 now. First install it using pip:
pip install django-bootstrap4

Add to INSTALLED_APPS in your settings.py:
'bootstrap4',

In your templates, load the bootstrap4 library and use the bootstrap_* tags:
{% load bootstrap4 %}

{# Display a form #}

<form action="/url/to/submit/" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">Submit</button>
{% endbuttons %}
</form>

* Reference: http://django-bootstrap4.readthedocs.io/en/latest/

#python #django #bootstrap #bootstrap4 #template django_bootstrap4