X
X
xxx2018-02-18 18:39:24
Django
xxx, 2018-02-18 18:39:24

How to pass the url of the page from which the request was sent to the hidden field of the form (ModelForm)?

Input data:
models

class User_information(models.Model):
    name = models.CharField(max_length=50)
    second_name = models.CharField(max_length=50)
    phone = PhoneNumberField()
    email = models.EmailField()
     url = models.SlugField()

forms
class PostForm(ModelForm):
    class Meta:
        model = User_information
        fields = ['name', 'second_name', 'phone', 'email', 'url']
        labels = {
            'name': _('Имя'),
            'second_name': _('Фамилия'),
            'phone': _('Телефон'),
            'email': _('EMAIL'),
        }
        widgets = {
            'name': Textarea(attrs={'cols': 80, 'rows': 3}),
           # 'url' : HiddenInput(), как добавить виджет скрытого поля
        }

Views
def detail(request, slug):
    html = 'post_detail.html'
    categories = Category.objects.all()
    query_set = get_object_or_404(Post, slug=slug)
    form = PostForm(request.POST or None)
    if form.is_valid():
        name = form.cleaned_data['name']
        second_name = form.cleaned_data['second_name']
        phone = form.cleaned_data['phone']
        email = form.cleaned_data['email']
        query_set = form.save(commit=False)
        query_set.save()
        # send_mail(name)
        form = PostForm()
        print(name, second_name, phone, email)
    context = {
        'detail': query_set,
        'categories': categories,
        "form": form,
    }
    return render(request, html, context)

How to auto-substitute in the hidden url field of the form page?
How to display the url of this page in the view?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Astrohas, 2018-02-18
@Astrohas

<input name="url"  value="{{request.get_full_path}}">

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question