Skip to content

uzayr-iqbal-hamid/learn-django

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typing SVG

Let's start by setting up a virtual environment.

In the terminal / VScode terminal:
Linux:

sudo apt install pipenv

Windows:

pip install pipenv

Then, common commands:

pipenv install


This installs all the necessary files (pipfile)

Install Django:

In terminal:

pipenv install Django

Open Shell:

pipenv shell

To ensure that you have installed Django, run:

pip show Django



Starting a new project:

In terminal:

django-admin startproject mysite .


Open it on localhost:8000

python manage.py runserver 0.0.0.0:8000


Migrate the changes:

python manage.py migrate


Create a super user:

python manage.py createsuperuser


Enter your username and password, confirm password.

To start the app:

python manage.py startapp feed

Go to settings.py, Add 'feed', item in the INSTALLED_APPS = []
image
In terminal

django-admin startproject mysite .

again.


Go to models.py, and type:

class Post(models.Model):
  text = models.CharField(max_length=140, blank=False, null=False)

In terminal:
python manage.py makemigrations
python manage.py migrate
python manage.py runserver 0.0.0.0:8000

Go to admin.py, and type:

  • from .models import Post
    
    class PostAdmin(admin.ModelAdmin):
      pass
    
    admin.site.register(Post, PostAdmin)

Refresh localhost:8000

Adding a home page

  • Go to views.py
    from django.views.generic import TemplateView

    from .models import Post
    class HomePage(TemplateView):
        http_method_names = ["get"]
        template_name = "feed/homepage.html"

        def dispatch(self, request, *args, **kwargs):
            self.request = request
            return super().dispatch(request, *args, **kwargs)
  • Make a new folder and file under feed, templates/feed/homepage.html
  • In mysite/urls.py,
    from django.contrib import admin
    from django.urls import path
    from django.conf.urls import include
    from feed import urls as feed_urls

    urlpatterns = [
        path('admin/', admin.site.urls),
        path("", include(feed_urls, namespace="feed")),
    ]
  • In settings.py, under INSTALLED_APPS, under DIRS[] add,
    'DIRS': [
            os.path.join(PROJECT_DIR, "feed/templates")
    ],

About

This repository contains the source code. Go through README.md to create a new django project

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages