Home>

Code in views.py:

def add_ads(request):
    form_add_ads= AdvertisementForm
    if request.method== 'POST':
        form_add= AdvertisementForm(request.POST)
        if form_add.is_valid:
            form_add.save(commit=False)
            form_add.instance.author= request.user
            form_add.instance.slug= get_slug(form_add.instance.title)
            form_add.save()
            return redirect('/ads/')
        else:
            form_add_ads= AdvertisementForm()
    return render(request, 'ads/add_ads.html', {'form_add_ads': form_add_ads})```
Code in models.py:
```class Advertisement(models.Model):
    title= models.CharField(
        'Ad name (must be unique, character max 100)',
        max_length=100, unique=True)
    content= models.TextField('Ad description')
    author= models.ForeignKey(User, on_delete=models.CASCADE)
    publish= models.DateTimeField(auto_now_add=True)
    category= models.ForeignKey('Category', on_delete=models.CASCADE)
    slug= models.SlugField(max_length=100, unique=True)
    class Meta:
        verbose_name= 'Announcement'
        verbose_name_plural= 'Announcements'
        ordering= ['publish']
    def __str__(self):
        return self.title
    def get_absolute_url(self):
        return reverse('ads_detail', args=[self.publish.year,
                                           self.publish.month,
                                           self.publish.day,
                                           self.slug])

Code in forms.py:

class Meta:
        model= Advertisement
        fields= ('title', 'content', 'category')

The essence of the question: what should I add to the view, to check if the header is unique? Tried in different ways, mostly with crutches, nothing works. The view itself adds an article to the database based on the form, which in turn is generated from the model. At the moment, when trying to create ads with a non-unique title, a natural error occurs:

PS: I just started learning django (2 weeks), I will be grateful for any comments in the code.

  • Answer # 1

    form_add.is_valid:

    is_validIt's not a property, it's a method. Add Parentheses

    Due to the usual inattention, nothing worked ... Thank you, you helped)

    Vladislav Shesternov2022-01-18 11:19:11
  • Answer # 2

    form_add.is_valid:

    is_validIt's not a property, it's a method. Add Parentheses

    Due to the usual inattention, nothing worked ... Thank you, you helped)

    Vladislav Shesternov2022-01-18 11:19:11
  • Answer # 3

    I don't understand a little why to use a unique description and name, if records by id in the database are already unique by default. But since the question is different, you can use the methodget_or_create, which adds an entry only if such an entry does not already exist, otherwise returns it from the database. Documentation:

    Example:

    adv= Advertisement.objects.get_or_create(title="Some title")
    # Accordingly, if you just need to add an entry, you can not write to the variable
    Advertisement.objects.get_or_create(title="Some Title")
    

    I hope I understood the problem correctly)

    The reason for the inoperability was my inattention) But thanks for the method, I'll take note in the future)

    Vladislav Shesternov2022-01-18 11:20:04

    Well, who does not happen)

    Nonen_Hook2022-01-18 14:47:49
  • Answer # 4

    I don't understand a little why to use a unique description and name, if records by id in the database are already unique by default. But since the question is different, you can use the methodget_or_create, which adds an entry only if such an entry does not already exist, otherwise returns it from the database. Documentation:

    Example:

    adv= Advertisement.objects.get_or_create(title="Some title")
    # Accordingly, if you just need to add an entry, you can not write to the variable
    Advertisement.objects.get_or_create(title="Some Title")
    

    I hope I understood the problem correctly)

    The reason for the inoperability was my inattention) But thanks for the method, I'll take note in the future)

    Vladislav Shesternov2022-01-18 11:20:04

    Well, who does not happen)

    Nonen_Hook2022-01-18 14:47:49