Trusted by Students Everywhere
Why Choose Us?
0% AI Guarantee

Human-written only.

24/7 Support

Anytime, anywhere.

Plagiarism Free

100% Original.

Expert Tutors

Masters & PhDs.

100% Confidential

Your privacy matters.

On-Time Delivery

Never miss a deadline.

Django Project!   1

Computer Science Mar 03, 2023

Django Project!

 

1. Make an action for a class in a Django project that will add 50 to the current value of a field (stock field) for the selected products and save the updated value in the database.

 

2. Define a 'register' view that allows a user to register as a Client.

 

3. 

def user_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)
        if user:
            if user.is_active:
                login(request, user)
                request.session['last_login'] = str(datetime.now())
                request.session.set_expiry(3600)
                return HttpResponseRedirect(reverse('myapp:index'))
            else:
                return HttpResponse("Your account is disabled.")
        else:
            return HttpResponse("Invalid login details")
    else:
        form = LoginForm()
        return render(request, 'myapp/login.html', {'form': form})

 

Update the user_login view above so that if a user who is not logged in goes to url '/myapp/myorders/' they will be directed to the login page and after successful login, they will go directly to the '/myapp/myorders/' page (instead of the main index page)

 

4. explain how to save the app database in JSON format. Load initial data using fixtures. 

 

5. Add validators for the stock field in Product model so that it is between 0 and 1000.

 

Product model in models.py

 

class Product(models.Model):
    category = models.ForeignKey(Category, related_name='products',
                                 on_delete=models.CASCADE)
    name = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    stock = models.PositiveIntegerField(default=100)
    available = models.BooleanField(default=True)
    description = models.TextField(blank=True, null=True)
    interested = models.PositiveIntegerField(default=0)

    def __str__(self):
        return self.name

    def refill(self):
        self.stock += 100
        self.save()

Expert Solution

For detailed step-by-step solution, place custom order now.
Need this Answer?

This solution is not in the archive yet. Hire an expert to solve it for you.

Get a Quote
Secure Payment