Coder Social home page Coder Social logo

sazzadulalambd / django_projects Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 93.65 MB

Python 82.03% HTML 1.46% Procfile 0.01% JavaScript 8.58% CSS 5.23% Shell 0.01% Batchfile 0.01% PowerShell 0.09% Xonsh 0.01% C 1.10% Cython 1.25% XSLT 0.21% Roff 0.01% TeX 0.02%

django_projects's Introduction

PyPI - Python Version

# django_projects 1. Install Django by running the following command in your terminal: ` pip install django `
  1. Create a new Django project by running the following command in your terminal:
django-admin startproject myproject
  1. Create a new Django app by running the following command in your terminal:
cd myproject
python manage.py startapp myapp
  1. Open the myproject/settings.py file and add the following lines to the INSTALLED_APPS list:
'myapp',
  1. Open the myapp/models.py file and define a model that represents the data you want to store in your database:
 from django.db import models
class MyModel(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField()
    phone = models.CharField(max_length=15)

    def __str__(self):
        return self.name
  1. Run the following command in your terminal to create the database table for your model:
python manage.py makemigrations myapp
python manage.py migrate
  1. Open the myapp/forms.py file and define a form that corresponds to your model:
from django import forms
from .models import MyModel

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ['name', 'email', 'phone']
  1. Open the myapp/urls.py file
from django.contrib import admin
from django.urls import path,include
from . import views

urlpatterns = [
    path('', views.home, name="home"),
    path('create', views.create, name="create"),
    path('list', views.list, name="list"),
    path('update', views.update, name="update"),
    path('delete', views.delete, name="delete"),
    
]
  1. Open the myapp/views.py file and define the views for your CRUD functionality:
from django.shortcuts import render, redirect, get_object_or_404
from .models import MyModel
from .forms import MyModelForm

def create(request):
    if request.method == 'POST':
        form = MyModelForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('list')
    else:
        form = MyModelForm()
    return render(request, 'create.html', {'form': form})


def list(request):
    mymodels = MyModel.objects.all()
    return render(request, 'list.html', {'mymodels': mymodels})


def update(request, pk):
    mymodel = get_object_or_404(MyModel, pk=pk)
    if request.method == 'POST':
        form = MyModelForm(request.POST, instance=mymodel)
        if form.is_valid():
            form.save()
            return redirect('list')
    else:
        form = MyModelForm(instance=mymodel)
    return render(request, 'update.html', {'form': form})


def delete(request, pk):
    mymodel = get_object_or_404(MyModel, pk=pk)
    mymodel.delete()
    return redirect('list')
  1. Create the HTML templates for your views. For example, create create.html:
{% extends 'base.html' %}

{% block content %}

<h1>Create a new model</h1>

<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Save</button>
</form>

<a href="{% url 'list' %}">Back to list</a>

{% endblock %}

In this example, we're extending a base template called 'base.html', which could include common elements like a header, footer, or navigation menu. The content block is where we'll put the form for creating a new person.

We've created an HTML form that uses the POST method to submit the form data to the server. We've also included the enctype="multipart/form-data" attribute, which is required when the form includes file uploads.

Inside the form, we're using the Django template tag csrf_token to add a security token to the form. This is a required security measure in Django to prevent Cross-Site Request Forgery (CSRF) attacks.

To display the form fields, we're using the {{ form.as_p }} template tag, which will render the form fields as paragraphs (<p>) with labels and input fields. You could also use {{ form.as_table }} to render the fields in a table, or {{ form.as_ul }} to render them as an unordered list.

list.html:

{% extends 'base.html' %}

{% block content %}

<h1>My models</h1>

<ul>

base.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>{% block title %}{% endblock %}</title>
</head>
<body>
 <header>
   <h1>My App</h1>
   <nav>
     <ul>
       <li><a href="{% url 'home' %}">Home</a></li>
       <li><a href="{% url 'create' %}">Add Person</a></li>
     </ul>
   </nav>
 </header>
 <main>
   {% block content %}{% endblock %}
 </main>
 <footer>
   &copy; My App {{ year }}
 </footer>
</body>
</html>

django_projects's People

Contributors

sazzadulalambd avatar

Stargazers

Ragib Shahriar avatar  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.