Tuesday, April 22, 2025

PHP First Page, Comments

You are advised to check corresponding YouTube video at the end of this article.

In PHP, the <?php and ?> tags are used to enclose PHP code in a file. These tags tell the web server that the enclosed code should be processed as PHP code, and not as plain text or HTML.

When the web server encounters the <?php tag, it starts interpreting and executing the PHP code until it reaches the ?> tag. Anything outside of the PHP tags is treated as plain text or HTML and is sent directly to the browser without any processing by the PHP engine.

The reason why we need to use the <?php and ?> tags when writing PHP code is to separate the PHP code from the rest of the content on the web page. This separation allows us to create dynamic web pages that include HTML, CSS, and JavaScript along with the PHP code.

It is important to note that in modern PHP development, the <?php tag is often omitted, and the code is enclosed in short tags <? and ?> or the echo tag <?=, which can make the code more concise and readable. However, this practice is discouraged as it can cause compatibility issues with different versions of PHP and web servers. 

Semicolons are used as statement terminators. A statement is a single line of code that performs a specific action, such as assigning a value to a variable, calling a function, or creating a loop. A semicolon is used to mark the end of a statement and to signal to the PHP interpreter that it should move on to the next statement.


<?php phpinfo(); ?>

<?php phpinfo(); ?> is a PHP code that outputs information about the PHP installation and configuration on the web server.

When this code is executed, it generates a large table of information that includes the PHP version, server information, modules and extensions, environment variables, and more. This information can be useful for troubleshooting PHP-related issues, checking PHP configuration settings, and optimizing PHP performance.

To view the output of <?php phpinfo(); ?>, we can create a new PHP file in the web server's document root directory, add the <?php phpinfo(); ?> code to the file, and access the file using a web browser by navigating to http://localhost/filename.php, where filename.php is the name of the PHP file we created.

It is important to note that we should not leave the <?php phpinfo(); ?> code in production environments, as it can reveal sensitive information about the server and PHP installation to potential attackers. Therefore, we should only use it for debugging and troubleshooting purposes in development environments. 


<?php 

echo "<h1>Mess with the best, die like the rest.</h1>";

//echo "<h1>Second Line</h1>";

echo "Third Line";

?>

The above PHP code uses the echo statement to output text to the browser.

The first line of the code uses the echo statement to output the HTML code <h1>Mess with the best, die like the rest.</h1> to the browser. The text is enclosed in double quotes to indicate that it is a string literal.

The second line of the code is commented out using //, which means that it will not be executed by the PHP interpreter. The comment is preceded by a double slash, which indicates to the interpreter that everything on the line after the double slash should be ignored.

The third line of the code uses the echo statement to output the string literal "Third Line" to the browser. Unlike the first line, this string is not enclosed in HTML tags, so it will be displayed as plain text on the page.

This code demonstrates how to use the echo statement to output text to the browser and how to use comments to provide context and documentation for the code.


<?php 

echo "<h1>Mess with the best, die like the rest.</h1>";

/*
echo "<h1>Second Line</h1>";

echo "Third Line";
*/

?>

In programming, comments are lines of text that are added to the source code of a program to provide additional information or explanations for other developers or future reference. Comments are not executed by the program and are ignored by the interpreter or compiler.

In PHP, there are two ways to create comments:

  1. Single-line comments: Single-line comments begin with two forward slashes //. Any text that follows on the same line after the // is treated as a comment and is ignored by the PHP interpreter.

Example: 

// This is a single-line comment in PHP
  1. Multi-line comments: Multi-line comments are enclosed in /* and */ characters. Any text that is enclosed in these characters is treated as a comment and is ignored by the PHP interpreter.

Example: 

/* This is a multi-line comment
   in PHP that can span multiple lines */

Comments can be used to provide explanations for complex or hard-to-understand code, to document code changes, or to temporarily disable a section of code during testing or debugging. They are an important tool for making code more readable, maintainable, and understandable for other developers.

PHP Localhost as WebServer

Running PHP on a local machine is essential for web development and deployment. 

Check YouTube tutorial embeded on this page, it's shortest way to learn how to set up PHP on localhost.

To run PHP on a remote server, we need to find a web hosting provider that supports PHP. There are many web hosting providers to choose from, and some of them even offer free hosting plans.

PHP Introduction

You are advised to check corresponding YouTube video at the end of this article.

PHP is a widely-used server-side scripting language that is designed for web development. It was first created in 1994 by Rasmus Lerdorf, and has since evolved into a powerful and versatile language that is used by millions of developers around the world.

One of the main advantages of PHP is its simplicity and ease of use. It has a relatively straightforward syntax that is similar to other C-based programming languages, making it accessible to new programmers. Additionally, PHP has a large and active community of developers who have created countless libraries, frameworks, and tools to help make web development easier and more efficient.

PHP is also known for its versatility and scalability. It can be used to build everything from simple websites to complex web applications and enterprise-level systems. Its ability to interact with databases like MySQL and PostgreSQL makes it a popular choice for building database-driven web applications, while its support for object-oriented programming (OOP) allows developers to build large and complex systems in a modular and scalable way.

In this course, we will explore the fundamentals of PHP, including its syntax, data types, control structures, functions, and so on. By the end of the course, you will have a solid understanding of PHP and be able to build your own web applications using this powerful language.

Django To-Do - Add New Post

You are advised to check corresponding YouTube video at the end of this article.

In views.py we need dedicated function that will submit things from our form into database:


from django.shortcuts import render, redirect
from .models import Posts

def index(request):
    all_posts = Posts.objects.all()[:10]
    context = {'all_posts' : all_posts}
    return render (request, 'index.html', context)

def details(request, id):
    one = Posts.objects.get(id=id)
    context = {'one' : one}
    return render (request, 'details.html', context)

def add(request):
    if(request.method == 'POST'):
        title = request.POST['title']
        text = request.POST['text']
        action = Posts(title = title, text = text)
        action.save()
        return redirect('index')
    else:
        return render(request, 'add.html')

def admin(request):
    response = redirect('admin')
    return response

The view function is called add, and it takes a request object as its argument. The function checks whether the HTTP request method is POST, which indicates that the form has been submitted with new post data.

If the request method is POST, the function retrieves the title and text fields from the request object using the request.POST dictionary. It then creates a new Posts object with the specified title and text fields, and saves it to the database using the save() method. Finally, it redirects the user to the index view function to display the updated list of posts.

If the request method is not POST, the function simply renders the add.html template, which displays a form for creating new posts.

This view function allows users to submit new posts to the database via a form, and redirects them back to the index page to see the updated list of posts.

That's it. Now you can add New Posts to a database.

Sure, you can grab this source and turn it into big site with a little bit of custom CSS, or if you are short on time, just use Bootstrap or some other frontend framework.

Django To-Do - Html Forms

You are advised to check corresponding YouTube video at the end of this article.

First, we will complete navigation in header.html:


<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>

  </head>
  <body>
    {% block navigation  %}
    <ul>
      <li><a href="{% url 'index' %}">Home</a></li>
      <li><a href="{% url 'add' %}">New Post</a></li>
      <li><a href="{% url 'admin' %}">Admin</a></li>
    </ul>
    {% endblock %}
    <hr>

Then, new route is needed in local urls.py file:


from django.urls import path
from .import views

urlpatterns = [
    path('', views.index, name='index'),
    path('details/<int:id>', views.details, name='details'),
    path('admin/', views.admin, name="admin"),
    path('add/', views.add, name='add'),
]

Generic space for new_post section in base.html:


{% include 'parts/header.html ' %}

{% block listing  %}
{% endblock %}

{% block details  %}
{% endblock %}

{% block new_post %}
{% endblock %}

{% include 'parts/footer.html ' %}

What about html form ? let's create add.html in templates directory:


{% extends 'base.html' %}

{% block new_post %}

<form action="{% url 'add' %}" method="post">
  {% csrf_token %}

  <label for="title">Title</label>
  <input type="text" name="title" id="title">
  <br><br>

  <label for="text">Text</label>
  <textarea name="text" rows="10" cols="80"></textarea>
  <br><br>

  <input type="submit" value="Submit">
</form>

{% endblock %}

Form is created, now we need to create a function in views.py file

Let's do that in next tutorial

Django To-Do - Redirects in Django

You are advised to check corresponding YouTube video at the end of this article.

Ok, let's create link to Django Admin in header.html


<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>

  </head>
  <body>
    {% block navigation  %}
    <ul>
      <li><a href="{% url 'index' %}">Home</a></li>
      <li><a href="">New Post</a></li>
      <li><a href="{% url 'admin' %}">Admin</a></li>
    </ul>
    {% endblock %}
    <hr>

Which means that local ulrs.py for our todo app demands attention:


from django.urls import path
from .import views

urlpatterns = [
    path('', views.index, name='index'),
    path('details/<int:id>', views.details, name='details'),
    path('admin/', views.admin, name="admin"),
]

Having a route is not enough, we must create function in views.py that will activate redirection in this case:


from django.shortcuts import render, redirect
from .models import Posts

def index(request):
    all_posts = Posts.objects.all()[:10]
    context = {'all_posts' : all_posts}
    return render (request, 'index.html', context)

def details(request, id):
    one = Posts.objects.get(id=id)
    context = {'one' : one}
    return render (request, 'details.html', context)

def admin(request):
    response = redirect('admin')
    return response

Now if we click on Admin in our navigation, redirect will be activated:

Also, we can put link to home page (or any other link) to the botom of details.html page, or even in footer.html, if you prefer that more.


{% extends 'base.html' %}

{% block details %}
<ul>
  <li>{{ one.id }}</li>
  <li>{{ one.title }}</li>
  <li>{{ one.text }}</li>
  <li>{{ one.created }}</li>
</ul>

<a href="{% url 'index' %}">Home</a>

{% endblock %}

Django To-Do - Post Details

You are advised to check corresponding YouTube video at the end of this article.

If we need to see post details on index.html, we need to fix it.

How? Well, every link needs to point to dedicated entry in database, which means, we must use IDs of posts:


{% extends 'base.html' %}
<h1>All Posts: </h1>

{% block listing %}

{% for post in all_posts %}
  <ul>
    <li><a href="details/{{ post.id }}">{{ post.title }} - {{ post.text }}</a></li>
  </ul>

{% endfor %}
{% endblock %}

To get post IDs as part of linking system, views.py must be changed:


from django.shortcuts import render
from .models import Posts

def index(request):
    all_posts = Posts.objects.all()[:10]
    context = {'all_posts' : all_posts}
    return render (request, 'index.html', context)

def details(request, id):
    one = Posts.objects.get(id=id)
    context = {'one' : one}
    return render (request, 'details.html', context)

Local urls.py for todo app must be changed too:


from django.urls import path
from .import views

urlpatterns = [
    path('', views.index, name='index'),
    path('details/<int:id>', views.details, name='details'),
]

It's time to create details.html page in templates directory:


{% extends 'base.html' %}

{% block details %}
<ul>
  <li>{{ one.id }}</li>
  <li>{{ one.title }}</li>
  <li>{{ one.text }}</li>
  <li>{{ one.created }}</li>
</ul>

{% endblock %}

Our base template (base.html) must hold block for details now:


{% include 'parts/header.html ' %}

{% block listing  %}
{% endblock %}

{% block details  %}
{% endblock %}

{% include 'parts/footer.html ' %}

Now you have index of posts on site homepage (index.html), that will lead visitors to dedicated details.html page for every post.

Django To-Do - Navigation

You are advised to check corresponding YouTube video at the end of this article.

In templates directory we will create parts directory with header.html and footer.html files.

Then, we need to include header and footer in base template, .base.html. Block section for listing will remain in base template, because custom pages will perhaps use that block.


{% include 'parts/header.html ' %}

{% block listing  %}

{% endblock %}

{% include 'parts/footer.html ' %}

index.html will remain uchanged:


{% extends 'base.html' %}
<h1>All Posts: </h1>

{% block listing %}

{% for post in all_posts %}
  <ul>
    <li>{{ post.id }} - {{ post.title }} - {{ post.text }}</li>
  </ul>

{% endfor %}
{% endblock %}

This will be in header.html:


<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>

  </head>
  <body>
    <ul>
      <li><a href="{% url 'index' %}">Home</a></li>
      <li><a href="">New Post</a></li>
      <li><a href="">Admin</a></li>
    </ul>
    <hr>

And footer.html will be minimal:


</body>
</html>

If you have a lot of code in header.html, consider having navigation in dedicated section:


<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>

  </head>
  <body>
    {% block navigation  %}
    <ul>
      <li><a href="{% url 'index' %}">Home</a></li>
      <li><a href="">New Post</a></li>
      <li><a href="">Admin</a></li>
    </ul>
    {% endblock %}
    <hr>

Django To-Do - Base Template

You are advised to check corresponding YouTube video at the end of this article.

Ok, let's create base.html in templates directory, with this content:


<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>

  </head>
  <body>
    <ul>
      <li><a href="">Home</a></li>
      <li><a href="">New Post</a></li>
      <li><a href="">Admin</a></li>
    </ul>
    <hr>
    
  </body>
</html>

Obviously, we need to include base.html in index.html, or any other custom page where navigation is desired:


{% extends 'base.html' %}

<h1>All Posts: </h1>

{% for post in all_posts %}
  <ul>
    <li>{{ post.id }} - {{ post.title }} - {{ post.text }}</li>
  </ul>

{% endfor %}

But if we do that with previous code - all posts are gone because there's no dedicated block of code inside index.html for for loop.

That means, our base.html must hold some generic section, that will be used/customized inside custom html pages that inherits from base template.


<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>

  </head>
  <body>
    <ul>
      <li><a href="">Home</a></li>
      <li><a href="">New Post</a></li>
      <li><a href="">Admin</a></li>
    </ul>
    <hr>

    {% block listing  %}

    {% endblock %}
    
  </body>
</html>

And now we can enclose for loop inside index.html in dedicated block/section:


{% extends 'base.html' %}

<h1>All Posts: </h1>

{% block listing %}

{% for post in all_posts %}
  <ul>
    <li>{{ post.id }} - {{ post.title }} - {{ post.text }}</li>
  </ul>

{% endfor %}

{% endblock %}

Now on index.html page we have navigation, but also all posts from our database.

Django To-Do - Listing Posts

You are advised to check corresponding YouTube video at the end of this article.

The view function is called index, and it takes a request object as its argument. The render function from Django's shortcuts module is used to generate an HTTP response by rendering a template with a context.

The view function starts by calling the all() method on the Posts model, which returns a queryset containing all instances of the Posts model in the database. The [:10] slice syntax is used to limit the queryset to the first 10 items, so that only the 10 most recent posts will be displayed.

The resulting queryset is stored in the all_posts variable, which is then included in a dictionary called context.

Finally, the render function is called with three arguments: the request object, the name of the template to render ('index.html'), and the context dictionary containing the queryset of posts. This generates an HTTP response containing the rendered template, which will display the 10 most recent posts on the page.

To list posts on homepage we need to fix views.py first:


from django.shortcuts import render
from .models import Posts

def index(request):
    all_posts = Posts.objects.all()[:10]
    context = {'all_posts' : all_posts}
    return render (request, 'index.html', context)


"""
def index(request):
    return render(request, 'index.html', {})
"""

With for loop we will extract posts for db and flush them in index.html:


<h1>All Posts: </h1>

{% for post in all_posts %}
  <ul>
    <li>{{ post.id }} - {{ post.title }} - {{ post.text }}</li>
  </ul>

{% endfor %}

In next tutorial we will work with base template, to prepare situation for multiple custom html pages/templates that will deal with posts from db in different ways.

Django To-Do - Improving Models

You are advised to check corresponding YouTube video at the end of this article.

Let's turn Postss in Posts, and also Post Titles deserve attention.

The __str__() method is a built-in Python method that is used to return a string representation of an object.

When defining a Django model, the __str__() method is often used to define the human-readable representation of the object. This is particularly useful when debugging or working with the Django admin interface, where you might need to quickly identify and work with specific instances of a model.


from django.db import models
from datetime import datetime

class Posts(models.Model):
    title = models.CharField(max_length = 200)
    text = models.TextField()
    created = models.DateTimeField(default=datetime.now)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = "Posts"

No migrations needed for this simple process, which is nice.

Django To-Do - Modeling Database

You are strongly advised to check corresponding YouTube video at the end of this article.

A model is a Python class that represents a database table. A Django model defines the fields and behaviors of the data that will be stored in a database, as well as the metadata required to manage the database table.

Django models are defined in a "models.py" file, typically located within a Django application. Each model is defined as a subclass of Django's "models.Model" base class, which provides it with a set of predefined methods and attributes for interacting with the database.

Models can define various types of fields, such as CharField for storing character strings, IntegerField for storing integers, DateField for storing dates, and so on. Additionally, models can specify various options such as default values, nullability, unique constraints, and more.

Once defined, Django models can be used to create database tables, query data from the database, and modify or delete existing data. Django's Object-Relational Mapping (ORM) provides a high-level, Pythonic interface for interacting with the database, allowing developers to work with data in a more natural and intuitive way.

Models are a crucial component of Django's framework for building web applications, allowing developers to easily define and manage the data structures that underlie their applications.

Navigate to models.py inside todo directory - we need to create structure of a database.


from django.db import models
from datetime import datetime

class Posts(models.Model):
    title = models.CharField(max_length = 200)
    text = models.TextField()
    created = models.DateTimeField(default=datetime.now)

The model inherits from Django's "models.Model" base class, which provides it with database-related functionality.

The "Posts" model has three fields defined: "title", "text", and "created".

The "title" field is a CharField with a maximum length of 200 characters, used to store the title of the post.

The "text" field is a TextField used to store the main text of the post.

The "created" field is a DateTimeField with a default value of the current date and time, used to store the date and time when the post was created.

This model could be used to represent a blog post in a Django application, allowing users to create and publish new posts, which would be stored in a database and displayed on the website.

Now, we need to prepare for migrations:


C:\todo_generic>python manage.py makemigrations
Migrations for 'todo':
  todo\migrations\0001_initial.py
    - Create model Posts

C:\todo_generic>

And than, migrations are possible:


C:\todo_generic>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, todo
Running migrations:
  Applying todo.0001_initial... OK

C:\todo_generic>

Let's run our server:


C:\todo_generic>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
July 27, 2020 - 15:42:23
Django version 3.0.8, using settings 'todo_generic.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

We need to be able to see new options in Django Administration

Go to admin.py inside todo directory, and put this there:


from django.contrib import admin
from .models import Posts

# Register your models here.
admin.site.register(Posts)

Now we can add New Posts using Admin Panel

But now, in Admin Panel we see Postss as option, and Posts object(1) for every entry.

Ok, we will fix that in next tutorial.

Django To-Do - Understanding Routing

You are advised to check corresponding youtube video at the end of this article.

Routing refers to the process of directing incoming HTTP requests to the appropriate view function. Routing allows you to map URLs to the code that will handle the request, allowing you to create dynamic and interactive web applications.

Routing is important in Django because it allows you to create clear and well-organized URLs for your application. Without routing, all requests would be directed to a single view function, which would then need to determine what action to take based on the URL provided. This could quickly become confusing and difficult to maintain as the application grows in size and complexity.

Routing also allows you to pass arguments and parameters to your views, which can be used to further customize the behavior of your application. For example, you might pass a user ID to a view function to display a specific user's profile page.

Go to C:\todo_generic\todo_generic where we need to add our todo app to settings.py

Section that we need to find inside settings.py is INSTALLED_APPS, add todo at the bottom of the list.


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'todo',
]

Then, inside todo directory create templates dierctory that will hold our html templates.

Inside templates directory create index.html page with a little bit of content for testing purposes. One h1 heading od simple paragraph will be enough.

Now, we need to fix views.py that will hold functions. They will be activated when visitors access specific paths on our website.


from django.shortcuts import render

def index(request):
    return render(request, 'index.html', {})

Also, inside todo directory, create routing table urls.py so that paths can trigger functions from views.py


from django.urls import path
from .import views

urlpatterns = [
    path('', views.index, name='index'),
]

Next thing, our local urls.py must be included in general urls.py inside todo_generic directory.


from django.contrib import admin
from django.urls import path, include

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

After we are done with routing, let's start our server, so we can access homepage (index.html) at 127.0.0.1:8000


C:\todo_generic>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
July 27, 2020 - 15:17:56
Django version 3.0.8, using settings 'todo_generic.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[27/Jul/2020 15:18:16] "GET / HTTP/1.1" 200 30
[27/Jul/2020 15:18:19] "GET / HTTP/1.1" 200 30
[27/Jul/2020 15:18:19] "GET / HTTP/1.1" 200 30
[27/Jul/2020 15:18:19] "GET / HTTP/1.1" 200 30

Django To-Do - Creating Django App


C:\>pip install django
Collecting django
  Using cached Django-3.0.8-py3-none-any.whl (7.5 MB)
Requirement already satisfied: pytz in c:\python38-64\lib\site-packages (from django) (2020.1)
Requirement already satisfied: asgiref~=3.2 in c:\python38-64\lib\site-packages (from django) (3.2.10)
Requirement already satisfied: sqlparse>=0.2.2 in c:\python38-64\lib\site-packages (from django) (0.3.1)
Installing collected packages: django
Successfully installed django-3.0.8

C:\>

The command "pip install django" is used to install Django. It is a package that can be installed using pip, a package installer for Python.

Pip is the standard package manager for Python, and it simplifies the process of installing and managing Python packages. By running the "pip install" command followed by the package name, you can download and install the package and all its dependencies automatically.

In the case of Django, running "pip install django" will download and install the latest version of Django available from the official Python Package Index (PyPI) repository, along with any other packages required for it to run. This command is essential if you want to start developing web applications using Django in Python. 


C:\>django-admin startproject todo_generic

C:\>

This command creates a new Django project named "todo_generic" in the current directory (C:). The "django-admin" is a command-line utility that ships with Django and provides various tools and utilities for creating and managing Django projects.

The "startproject" option is used to create a new Django project, and "todo_generic" is the name of the project. This command creates a directory named "todo_generic" in the current directory and populates it with the basic files and directories required to start a Django project. 


C:\todo_generic>dir
 Volume in drive C is New Volume
 
 Directory of C:\todo_generic

07/27/2020  02:14 PM    <DIR>          .
07/27/2020  02:14 PM    <DIR>          ..
07/27/2020  02:14 PM               653 manage.py
07/27/2020  02:14 PM    <DIR>          todo_generic
               1 File(s)            653 bytes
               3 Dir(s)  77,539,221,504 bytes free

C:\todo_generic>

C:\todo_generic>cd todo_generic

C:\todo_generic\todo_generic>dir
 Volume in drive C is New Volume
 Volume Serial Number is B082-0A47

 Directory of C:\todo_generic\todo_generic

07/27/2020  02:14 PM    <DIR>          .
07/27/2020  02:14 PM    <DIR>          ..
07/27/2020  02:14 PM               417 asgi.py
07/27/2020  02:14 PM             3,226 settings.py
07/27/2020  02:14 PM               775 urls.py
07/27/2020  02:14 PM               417 wsgi.py
07/27/2020  02:14 PM                 0 __init__.py
               5 File(s)          4,835 bytes
               2 Dir(s)  77,534,949,376 bytes free

C:\todo_generic\todo_generic>

C:\todo_generic>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
July 27, 2020 - 14:19:22
Django version 3.0.8, using settings 'todo_generic.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

This command starts the development server of the Django project. When you run this command, Django starts the server and makes your application available at a specific URL (by default, http://localhost:8000/). You can then use a web browser to visit that URL and interact with your application. The server listens for incoming requests, processes them, and sends back responses. You can use Ctrl + C in the command prompt to stop the server.

Cancel our server for now, because we need to create dedicated todo app inside main todo_generic project.


C:\todo_generic>python manage.py startapp todo

C:\todo_generic>

This command creates a new Django app named todo in the current directory (C:\todo_generic). The app's directory structure will be created automatically with a set of default files including an admin.py, models.py, tests.py, and views.py file. This command is used to create a new app in a Django project. 


C:\todo_generic>cd todo

C:\todo_generic\todo>dir
 Volume in drive C is New Volume
 Volume Serial Number is B082-0A47

 Directory of C:\todo_generic\todo

07/27/2020  02:24 PM    <DIR>          .
07/27/2020  02:24 PM    <DIR>          ..
07/27/2020  02:24 PM                66 admin.py
07/27/2020  02:24 PM                88 apps.py
07/27/2020  02:24 PM    <DIR>          migrations
07/27/2020  02:24 PM                60 models.py
07/27/2020  02:24 PM                63 tests.py
07/27/2020  02:24 PM                66 views.py
07/27/2020  02:24 PM                 0 __init__.py
               6 File(s)            343 bytes
               3 Dir(s)  77,550,043,136 bytes free

C:\todo_generic\todo>

C:\todo_generic\todo>cd..

C:\todo_generic>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

C:\todo_generic>

The python manage.py migrate command in Django is used to apply all the migrations to the database. It syncs the database schema with the current set of models and also updates the existing tables with any changes made to the models.

This command is used after creating new models or making changes to the existing models in Django.

In this specific case, the command is being run in the todo_generic project directory to apply any migrations that may be required based on the models defined in the project. 


C:\todo_generic>python manage.py createsuperuser
Username (leave blank to use 'user'): admin
Email address: admin@server.com
Password:
Password (again):
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.

C:\todo_generic>

This command is used to create a superuser account for the Django application. A superuser has all the permissions and access to the Django application, including the ability to add, modify or delete any data.

When this command is executed, Django will prompt the user to enter a username, email address, and password for the superuser. Once the user provides this information, the superuser account will be created and can be used to log in to the Django application's admin panel. 


C:\todo_generic>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
July 27, 2020 - 14:28:56
Django version 3.0.8, using settings 'todo_generic.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Now we can access homepage at http://127.0.0.1:8000, but also admin panel (Django Administration) is available at http://127.0.0.1:8000/admin

Go login with admin/pass combination you choose. In next tutorial we will understand routing in Django.

Django To-Do - First Simple CMS

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...