Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Django Project!   1

Django Project!   1

Computer Science

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()

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Related Questions