#need help with doing this
22 messages · Page 1 of 1 (latest)
Most questions answered on this Discord is best backend up using text, code, and logs, neither of which make sense to share as images. If you definitely feel you need to share an image to get your question across, upload it to one of many image sharing websites and share a link here.
All i see is a pelican 🙃
The requested page could not be found
Please do not share pictures of text. I will be deleting these.
there's a table can't send text
Task 1: using models, we create a database scheme
Step 1: To have the scheme in the database, we write the following in ‘apps/ bookmodule/models.py’, Note that: ID is done automatically by Django, so we do not have to explicitly define it in the model.
from django.db import models
class Book(models.Model):
title = models.CharField(max_length = 50)
author = models.CharField(max_length = 50)
price = models.FloatField(default = 0.0)
edition = models.SmallIntegerField(default = 1)
Step 2: Using manage.py from the command line, to make necessary migrations (modification that the database need to do):
python manage.py makemigrations
Step 3: Again using manage.py from the command line, apply the migrations to the database:
python manage.py migrate
Task 2: use the application ‘DB Browser for SQLite’ or ‘VS code’ to view the database and insert fake data into it.
Note: For VS code, you need SQLite extension, and for SQLite, you will use sqlitebrowser at (https://sqlitebrowser.org/dl/). In the lab, we assumed that you are familiar with at least one of them.
Step 1: Open the database ‘db.sqlite3’ file in ‘VS code’ or ‘DB Browse for SQLite’ apps you can see the table.
Step 2: For development purposes, you would need data, and you can use random data generator websites, like: www.mockaroo.com
Step 3: Export the data to any file you want; I recommend SQL. Then, import that data into your database. You can use DB SQLite Browser, with the following steps:
· Open db.sqlite3
· Tab to execute SQL window
· Paste the content of SQL downloaded file into the windows
· Hit execute
Step 4: Also, to insert data from the Django from any view function, you can use the constructor function or create function:
· Use the constructor function
mybook = Book(title = 'Continuous Delivery', author = 'J.Humble and D. Farley', edition = 1)
· Use the create function
mybook = Book.objects.create(title = 'Continuous Delivery', author = 'J.Humble and D. Farley', edition = 1)
Either way, we must save the object once created:
mybook.save()
Task 3: Use simple queries using Django ORM.
Step 1: In ‘apps/bookmodule/views.py’, import book model:
from .models import Book
Step 2: Create URL ‘/books/simple/query’ with a view function ‘simple_query’ in ‘apps/bookmodule/views.py’:
def simple_query(request):
mybooks=Book.objects.filter(title__icontains='and') # <- multiple objects
return render(request, 'bookmodule/bookList.html', {'books':mybooks})
Step 3: In 'bookmodule/bookList.html', add Django tag to list books:
{% for obj in books %}
ID: {{obj.id}}, Title: {{obj.title}}
<hr>
{% endfor %}
Step 4: Now from the web browser, navigate to http://127.0.0.1:8000/books/simple/query
Task 4: Customize queries using common lookup conditions and keywords.
Step 1: Create URL ‘/books/complex/query’ with a view function ‘lookup_query’ in ‘apps/bookmodule/views.py’:
def complex_query(request):
mybooks=books=Book.objects.filter(author__isnull = False).filter(title__icontains='and').filter(edition__gte = 2).exclude(price__lte = 100)[:10]
if len(mybooks)>=1:
return render(request, 'bookmodule/bookList.html', {'books':mybooks})
else:
return render(request, 'bookmodule/index.html')
Step 2: Now from the web browser, navigate to http://127.0.0.1:8000/books/complex/query
@spiral forge @stark urchin
anyone?
I'm in bed already, but please explain what you have done, which step you are having problems with, what is the exact problem you are having, etc.
Also, check #rules ... rule 8 states
When helping with homework, help people learn how to do the assignment without doing it for them.
all, I'm not asking to do them for me, help me understand so i do them by myself