0% found this document useful (0 votes)
84 views5 pages

Django Project

The document contains code snippets from a Django e-commerce website that allows users to view products, sign up, log in, and log out. It includes views, templates, models, and settings to display products, manage user authentication and accounts, and link all functionality together. Key aspects covered are retrieving products and users from the database, rendering templates with context based on requests, and defining URLs and authentication.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views5 pages

Django Project

The document contains code snippets from a Django e-commerce website that allows users to view products, sign up, log in, and log out. It includes views, templates, models, and settings to display products, manage user authentication and accounts, and link all functionality together. Key aspects covered are retrieving products and users from the database, rendering templates with context based on requests, and defining URLs and authentication.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

View In Store

from [Link] import render, get_object_or_404, redirect

from .models import Product

def index(request):
products = [Link]()
return render(request, 'store/[Link]', context={'products': products})
# Create your views here.

def product_list(request, slug):


products = get_object_or_404(Product, slug = slug)
return render (request, 'store/[Link]',context={'product':products})
View In Accounts
from [Link] import render, redirect
from [Link] import get_user_model, login, logout, authenticate
# Create your views here.
User = get_user_model()
def signup(request):
if [Link] == "POST":
username = [Link]("username")
password = [Link]("password")
user = [Link].create_user(username=username, password=password)
login(request, user)
return redirect("home")
return render(request, 'accounts/[Link]')

def login_user(request):
if [Link] == "POST":
username = [Link]("username")
password = [Link]("password")
user = authenticate(username=username,password=password)
if user:
login(request, user)
return redirect('home')
return render(request, 'accounts/[Link]')
def logout_user(request):
logout(request)
return redirect('home')
Sign Up and Sign in (html files)

{% extends '[Link]' %}
{% block content %}
<h1>Inscription to the Site</h1>
<form method="POST">{% csrf_token %}
<p><label for="username">Username</label>
<input id="username" type="text" name="username">
</p>
<p><label for="password">Password</label>
<input id="password" type="password" name="password">
</p>
<button type="submit">Submit</button>
</form>
{% endblock content %}

List Products(html)

{% extends '[Link]' %}
{% block content %}
<h3>{{ [Link] }}</h3><br>
<h4>{{ [Link] }}Kz</h4>
<img src="{{ [Link] }}" alt="{{ [Link] }}" style="max-width: 150px">
<p>{{ [Link] }}</p>
<p>{{ [Link] }} in Stock</p>
{% endblock content %}

Index (html)

{% extends '[Link]' %}
{% block content %}
{% for product in products %}
<h3>{{ [Link] }}</h3>
<img src="{{ [Link] }}" alt="{{ [Link] }}" style="max-width:
150px;">
{# <a href="{% url 'product_list' [Link] %}">Detail</a>#}
<a href="{{ product.get_absolute_url }}">Product detalis</a>
{% endfor %}
{%endblock content %}
Function to authenticate and connect User in Database

<li class="nav-item"> {% if user.is_authenticated %}</li>


<li class="nav-item" ></li>
<li class="nav-item" ><a class="nav-link" href="{% url 'logout' %}">signout (Connected
as: {{ [Link] }})</a></li>

[Link] file

from [Link] import static


from [Link] import admin
from [Link] import path
from [Link] import index,product_list
from [Link] import signup, logout_user, login_user
from shop import settings

urlpatterns = [
path("admin/", [Link]),
path("", index, name="home"),
path("signup", signup, name="signup"),
path("logout", logout_user, name="logout"),
path("login", login_user, name="login"),
path("product_list/<str:slug>/", product_list, name="product_list")
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

[Link] file
Variables to add:
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / "media"
# AUTH_USER_MODEL EST LA VARIABLE QUI ME PERMET D'AUTHENTIFIER APP accounts ET
LA CLASSE Shopper
AUTH_USER_MODEL="[Link]"
Models files

In store App:
from [Link] import models
from [Link] import reverse

class Product([Link]):
name = [Link](max_length=128)
slug = [Link](max_length=128)
price = [Link](default=0.0)
stock = [Link](default=0)
description = [Link](blank=True)
thumbnail = [Link](upload_to="products", blank=True, null=True)
def __str__(self):
return [Link]
def get_absolute_url(self):
return reverse('product_list', kwargs={'slug': [Link]})
In accounts App:
from [Link] import AbstractUser

class Shopper(AbstractUser):
pass
[Link] files
In store:
from [Link] import admin
from .models import Product

class AdminProduct([Link]):
list_display = ['name', 'price', 'stock']

[Link](Product, AdminProduct)
In Accounts:
from [Link] import admin
from .models import Shopper
[Link](Shopper)

You might also like