Mohammad Abdollahzadeh
Mohammad Abdollahzadeh

Reputation: 420

how to access forms field in django?

This is my forms:

class signup_form(forms.ModelForm):
    bio = forms.TextInput()
    class Meta:
        model = User
        fields = ['username',
                  'password',
                  'first_name',
                  'last_name', 
                  'email',
                  'date_joined']

And This one is my template page:

urlpatterns = [
    ......
    url(r'^profile/(?P<username>[\w\-]+)/$', user_profile, name='user_profile'),
]

And this is signup template page:

{% extends parent_template|default:"tick/base_tick.html" %}

{% block title %}
{{ block.super }} ---> Sign Up HERE!!
{% endblock %}


{% block content %}
    <div>
        <div>
            <form action="{% url 'user_profile' username={{ form.username }} %}" method="post">
                {% csrf_token %}
                {{form.as_p}}
                <button type="submit">Create User</button>
            </form>
        </div>
    </div>
{% endblock %}

As you can see in the 'action' part of the form i want to access to the 'username' field of 'form' but i can't and the Django get me some error.

What Should I do?

Edit: This is the Error enter image description here

Upvotes: 0

Views: 1025

Answers (1)

SHIVAM JINDAL
SHIVAM JINDAL

Reputation: 2974

Value of a field is accessed by form.field_name.value. Use can update your code by below code

<form action="{% url 'user_profile' username=from.username.value %}" method="post">
    {% csrf_token %}
    {{form.as_p}}
    <button type="submit">Create User</button>
</form>

Upvotes: 1

Related Questions