Django 1st app
Django 1st app
Here are some of the key features that make Django a powerful choice
for web development:
To get started with Django, you need to have Python installed on your
system. Here are the basic steps to set up a Django project:
sh
pip install django
sh
sh
Virtual Environment
Step-by-Step Guide
sh
python --version
If you don't have Python installed, you can download it from the
official Python website.
sh
virtualenv myenv
Replace myenv with the name you want for your virtual
environment.
sh
myenv\Scripts\activate
sh
source myenv/bin/activate
Before you begin, ensure you have Python and virtualenv installed. Set
up a virtual environment to manage your project dependencies and keep
your project isolated.
sh
python manage.py startapp appname
5.Defining Models
Define your database models in the models.py file of your app. Models
represent the structure of your data and allow Django to generate SQL
queries automatically.
sh
python manage.py makemigrations
python manage.py migrate
7. Creating Views
Define your views in the views.py file of your app. Views handle user
requests and return responses, such as rendering templates or returning
JSON data.
8. Mapping URLs
Create URL patterns in the urls.py file of your app to map views to
specific URLs. Include your app’s URL patterns in the project’s main
urls.py file.
9. Creating Templates
sh
python manage.py runserver
Add static files (e.g., CSS, JavaScript, images) to your project in a static
directory. Configure static file handling in your settings.py file.
Write tests for your app in the tests.py file. Use Django’s testing
framework to ensure your code works as expected:
sh
python manage.py test
This directory is the container for your entire Django project. It holds the
settings, URL configurations, and other project-level components.
myapp/:
o __init__.py: Makes this directory a Python package.
o admin.py: Used for registering your models with the Django
admin interface. This makes your models manageable
through the admin panel.
o apps.py: Contains configuration for your app.
o migrations/: This directory stores migration files. These files
are how Django tracks and applies changes to your
database schema (when you modify your models).
__init__.py: Empty, makes the migrations directory a
package.
o models.py: Defines the data models for your app. These
models represent the structure of the data you'll be storing in
your database.
o tests.py: Used for writing unit tests for your app.
o views.py: Contains the view functions (or classes) that
handle incoming web requests and return responses (usually
HTML, JSON, etc.). This is where the logic of your app
resides.
o urls.py (Optional, but Highly Recommended): You can
create a urls.py file inside your app to define URL patterns
specific to that app. This is good practice for organizing your
URLs. If you don't have this file, you'll define all your URL
patterns in the project-level urls.py.
Example Workflow:
Example:Hello world
sh
python --version
sh
sh
virtualenv myenv
o On Windows:
sh
myenv\Scripts\activate
o On macOS/Linux:
sh
source myenv/bin/activate
sh
pip install django
sh
django-admin startproject myproject1
cd myproject
sh
python manage.py startapp myapp
python
# myproject/settings.py
INSTALLED_APPS = [
...
'myapp',
]
1. myapp/views.py:
def home(request):
return HttpResponse("hello world")
2. myapp/urls.py:
python
urlpatterns = [
path(' ', views.home, name='home'),
]
3. myproject/urls.py:
python
urlpatterns = [
path('admin/', admin.site.urls),
path(' ', include('myapp.urls')),
]
Visit http://127.0.0.1:8000