🌍 Python · flashcards
Python Django E-commerce Project Flashcards
51 question-and-answer cards covering Django E-commerce Project as it is examined in Python. 24 of them are printed below, taken from across the deck — no signup, no paywall on the preview.
24 sample cards from the Django E-commerce Project deck
Sampled from the end of the deck, so these are different cards from the ones shown on the syllabus page.
What is the standard pattern for a one-to-one user profile model?
Define a `Profile` model with `user = models.OneToOneField(User, on_delete=models.CASCADE)` plus extra fields, so each user has exactly one profile accessed via `user.profile`.
How can you automatically create a profile whenever a new user registers?
Connect a `post_save` signal on the `User` model: when `created` is True, create the related `Profile`. Register the receiver in the app's `apps.py` `ready()` method or `signals.py`.
What is the difference between `request.user` for an authenticated vs. anonymous request?
For a logged-in request, `request.user` is a `User` instance with `is_authenticated == True`. For an anonymous request it is an `AnonymousUser` instance whose `is_authenticated` is `False`.
Write a minimal `Product` model with name, price, and stock fields.
```python class Product(models.Model): name = models.CharField(max_length=200) price = models.DecimalField(max_digits=10, decimal_places=2) stock = models.PositiveIntegerField(default=0) ```
Why use `DecimalField` rather than `FloatField` for product prices?
`DecimalField` stores exact fixed-point decimals, avoiding the binary floating-point rounding errors of `FloatField`—essential for monetary values. You specify `max_digits` and `decimal_places`.
How do you link a `Product` to a `Category` so each product belongs to one category?
Add `category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='products')`. This creates a many-to-one relation; access a category's products via `category.products.all()`.
What field type and setting let a product have a clean URL-friendly identifier?
A `SlugField`, e.g. `slug = models.SlugField(unique=True)`. It stores a URL-safe string (letters, numbers, hyphens) used to build product detail URLs.
Why is defining `get_absolute_url()` on the `Product` model useful?
It returns the canonical URL for a product instance (via `reverse()`), letting templates and the admin link to it with `{{ product.get_absolute_url }}` and centralizing URL logic.
What is the difference between a function-based `ListView` written manually and Django's generic `ListView` for product listing?
A manual function view calls `Product.objects.all()` and renders a template explicitly. The generic `ListView` sets `model = Product` (or a `queryset`) and handles fetching, pagination, and context automatically via `object_list`.
How do you paginate a product listing in a function-based view?
Use `django.core.paginator.Paginator`: `paginator = Paginator(products, 12)`, then `page_obj = paginator.get_page(request.GET.get('page'))`, and pass `page_obj` to the template.
How do you query only the products that are in stock for a listing view?
Filter the queryset, e.g. `Product.objects.filter(available=True, stock__gt=0)`. The `__gt` lookup means "greater than" so only products with stock above zero are returned.
What does a product detail view need to do, and how does it fetch the right product?
It retrieves a single product (typically by slug or pk) and renders its full information. Use `get_object_or_404(Product, slug=slug)` so a missing product returns a 404 instead of an exception.
In a URL pattern, how do you capture a product's primary key to pass to a detail view?
Use a path converter: `path('product/<int:pk>/', views.product_detail, name='product_detail')`. The `<int:pk>` captures an integer and passes it as the `pk` keyword argument.
What does `get_object_or_404` do and why is it preferred over a plain `get()`?
It calls the manager's `get()` and raises `Http404` (a proper 404 page) if no object matches, instead of letting `DoesNotExist` propagate as a 500 server error.
Describe two common strategies for storing a shopping cart in Django.
1) Session-based: store cart data (product ids and quantities) in `request.session`—works for anonymous users, no DB model needed. 2) Database-based: `Cart` and `CartItem` models tied to a user—persists across devices and sessions.
Define a `CartItem` model that links a cart, a product, and a quantity.
```python class CartItem(models.Model): cart = models.ForeignKey(Cart, on_delete=models.CASCADE, related_name='items') product = models.ForeignKey(Product, on_delete=models.CASCADE) quantity = models.PositiveIntegerField(default=1) ```
For a cart item with unit price $p$ and quantity $q$, what expression gives its line subtotal?
The line subtotal is $p \times q$. The cart total is the sum over all items: $$\text{total} = \sum_{i=1}^{n} p_i \cdot q_i$$
What is the core logic of an 'add to cart' view?
Get the product (e.g. `get_object_or_404`), read the requested quantity, then either create a new cart item or increment the quantity of an existing one—commonly via `get_or_create`—save it, and redirect to the cart page.
How does `get_or_create` simplify add-to-cart logic?
`CartItem.objects.get_or_create(cart=cart, product=product)` returns `(item, created)`. If the item exists you get it back (`created=False`) and increment its quantity; otherwise it creates a new one—avoiding manual existence checks.
After processing an add-to-cart POST, why return a redirect instead of rendering directly?
To follow the Post/Redirect/Get pattern: redirecting prevents the form from being re-submitted if the user refreshes the page, avoiding accidental duplicate additions.
What must an 'update cart' view do when a user changes an item's quantity to zero?
If the new quantity is zero (or less), delete the `CartItem` rather than saving a zero-quantity row; otherwise update `item.quantity` and save. Then redirect back to the cart view.
How does a cart view typically compute and display the grand total?
It iterates the cart's items, multiplies each item's `product.price` by its `quantity` for line totals, and sums them—often via a model method `get_total()` returning `sum(item.get_subtotal() for item in self.items.all())`.
What two management commands apply model changes to the database, and what does each do?
`python manage.py makemigrations` generates migration files from model changes; `python manage.py migrate` applies those migration files to the database schema.
What command creates an admin superuser so you can manage products and orders via Django admin?
`python manage.py createsuperuser`. It prompts for username, email, and password, then you log in at `/admin/` to manage registered models.
What this deck covers
The Django E-commerce Project deck follows the Python Django E-commerce Project syllabus — 11 chapters and 33 topics — so questions land on material that is genuinely examinable rather than trivia around it. That works out to roughly 4.6 cards per chapter.
Answers are written to be recallable, not just readable — averaging about 187 characters, which is long enough to carry the reasoning and short enough to say out loud.
A deck like this earns its keep on the second and third pass. Read the syllabus first so you know the shape of the subject, then use the cards to find the specific facts that have not stuck.
Django E-commerce Project flashcards FAQ
How many Django E-commerce Project flashcards are in this Python deck?
51 cards. This page previews 24 of them, sampled evenly across the deck so you can judge the difficulty before installing anything.
Are these Python flashcards free?
Yes. The preview here is free to read with no signup, and the full 51-card deck is free inside the Examius app.
What do the Django E-commerce Project cards cover?
They follow the Python Django E-commerce Project syllabus — 11 chapters and 33 topics — so the questions track what is actually examinable.
How should I use these flashcards?
Read the syllabus first so you know the shape of the subject, then drill the deck. Examius schedules each card with spaced repetition, so cards you keep missing come back sooner and ones you know drift further apart.