Giter Club home page Giter Club logo

basic-django-blog-app's Introduction

Basic django blog application.

Setting up the Django Project and App First, ensure Django is installed and set up a new Django project and app:

Create a New Project (if not already created):

django-admin startproject mysite
cd mysite

Create a New App (e.g., 'blog'):

python manage.py startapp blog

Include the App in Your Project:

Open mysite/settings.py. Add 'blog' to the INSTALLED_APPS list.

  1. Defining the Model In the blog app, modify the models.py file to define the BlogPost model:
from django.db import models

class BlogPost(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    published_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title
        ```
Migrate the Model:
Run python manage.py makemigrations blog to create migrations for the changes.
Run python manage.py migrate to apply migrations to the database.

3. Creating Views and URL Routing
Define a View in views.py of the blog app:

from django.shortcuts import render from .models import BlogPost

def blog_list(request): posts = BlogPost.objects.all().order_by('published_date') return render(request, 'blog/blog_list.html', {'posts': posts})

Set up URL Routing:

Create a urls.py in the blog app directory.

Define the URL pattern for the view:

from django.urls import path from . import views

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

Include blog URLs in the main URLconf (mysite/urls.py):

from django.urls import include, path

urlpatterns = [ path('blog/', include('blog.urls')), # other paths... ]


4. Designing the Template
Create a Template File:

Create a directory named templates within the blog app.
Inside templates, create another directory named blog.
Create a file blog_list.html inside blog/templates/blog/.
Design the Template (blog_list.html):
<title>Blog</title>

Blog Posts

    {% for post in posts %}
  • {{ post.title }}

    {{ post.content }}

    Published on: {{ post.published_date }}

  • {% endfor %}
````
  1. Registering with the Admin Interface Register the Model in admin.py of the blog app:
from django.contrib import admin
from .models import BlogPost

admin.site.register(BlogPost)

Create a Superuser and Test the Admin Interface:

Run python manage.py createsuperuser and follow the prompts. Run the server python manage.py runserver. Go to http://127.0.0.1:8000/admin, log in, and test adding, editing, and deleting blog posts. Running the Application Start the Django development server:

python manage.py runserver Access the blog list view by navigating to http://127.0.0.1:8000/blog/.

basic-django-blog-app's People

Contributors

vitamaxoduol avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.