forms.py
class GroupForm(ModelForm): class Meta: model = GroupsList fields = ['link'] def clean(self): link = self.cleaned_data.get('link') vk_api = get_api() info = get_grop_info(link, vk_api) if info['count']: if info['count'] < 0: raise ValidationError('В группе нет участников') elif info['count'] > 10000: raise ValidationError('Слишком нмого участников >10000') else: raise ValidationError('Проблема с получением данных') return self.cleaned_data
vievs.py
def group_add(request, username): if request.POST: user = User.objects.get(username=username) form = GroupForm(request.POST) if form.is_valid(): input_group = form.save(commit=False) group_link = input_group.link group_info = get_grop_info(input_group.link, get_api()) try: group = GroupsList.objects.get(link=group_link) try: group.author.add(user.id) except Exception as e: print(e) insert_group_info(input_group.link, group_info) except: input_group = form.save() group_link = input_group.link group = GroupsList.objects.get(link=group_link) group.author.add(user.id) insert_group_info(input_group.link, group_info) else: return redirect('/', {'group_form': form}) return redirect('/', {'group_form': form})
index.html
<form action="manage_group/group_add/{{ username }}/" method="post"> {{ group_form.as_p }} <input type="submit" value="Добавить группу"> {% csrf_token %} </form>