[go: up one dir, main page]

0% found this document useful (0 votes)
1 views7 pages

Backend

The document outlines the URL configuration and models for a Django project named 'my_app', including the setup for handling views and feedback related to watches. It provides examples of how to route URLs to both function and class-based views, as well as the structure of the Watch and Feedback models. Additionally, it includes a function to generate QR codes for watches, saving them as images in the database.

Uploaded by

dhruvzack0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views7 pages

Backend

The document outlines the URL configuration and models for a Django project named 'my_app', including the setup for handling views and feedback related to watches. It provides examples of how to route URLs to both function and class-based views, as well as the structure of the Watch and Feedback models. Additionally, it includes a function to generate QR codes for watches, saving them as images in the database.

Uploaded by

dhruvzack0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Backend :

URL configuration for my_app project.

The `urlpatterns` list routes URLs to views. For more


information please see:

https://docs.djangoproject.com/en/5.1/topics/http/url
s/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home,
name='home')
Class-based views
1. Add an import: from other_app.views import
Home
2. Add a URL to urlpatterns: path('', Home.as_view(),
name='home')
Including another URLconf
1. Import the include() function: from django.urls
import include, path
2. Add a URL to urlpatterns: path('blog/',
include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
path('admin/', admin.site.urls),
]

#!/usr/bin/env python
"""Django's command-line utility for administrative
tasks."""
import os
import sys

def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE',
'my_app.settings')
try:
from django.core.management import
execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's
installed and "
"available on your PYTHONPATH environment
variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)

if __name__ == '__main__':
main()
from rest_framework import serializers
from .models import Watch, Feedback

class FeedbackSerializer(serializers.ModelSerializer):
class Meta:
model = Feedback
fields = ['id', 'watch', 'user_name', 'feedback_text',
'created_at']

class WatchSerializer(serializers.ModelSerializer):
feedbacks = FeedbackSerializer(many=True,
read_only=True)

class Meta:
model = Watch
fields = ['id', 'name', 'brand', 'model', 'qr_code',
'feedbacks']
from django.db import models

# Create your models here.


from django.db import models

class Watch(models.Model):
name = models.CharField(max_length=100)
brand = models.CharField(max_length=100)
model = models.CharField(max_length=100)
qr_code = models.CharField(max_length=255,
unique=True) # Stores the QR code string

def __str__(self):
return f"{self.brand} {self.model}"

class Feedback(models.Model):
watch = models.ForeignKey(Watch,
related_name='feedbacks',
on_delete=models.CASCADE)
user_name = models.CharField(max_length=100)
feedback_text = models.TextField()
created_at =
models.DateTimeField(auto_now_add=True)

def __str__(self):
return f"Feedback for {self.watch}"

import qrcode
from io import BytesIO
from django.core.files.base import ContentFile
from .models import Watch

def generate_qr_code(watch_id):
watch = Watch.objects.get(id=watch_id)
qr = qrcode.QRCode(
version=1,

error_correction=qrcode.constants.ERROR_CORRECT_L
,
box_size=10,
border=4,
)
qr.add_data(watch.qr_code)
qr.make(fit=True)
img = qr.make_image(fill='black', back_color='white')
buffer = BytesIO()
img.save(buffer, format='PNG')
file_name = f"{watch_id}_qr.png"
watch.qr_code.save(file_name,
ContentFile(buffer.getvalue()))
watch.save()

You might also like