Reputation: 395
So I have these working views for registration and login using POST requests, both of which returning profile.html.
The problem is the login and registration views return a POST to the login or register urls when i submit the forms(according to cmd), to run the necessary functions. which means it is simply rendering the profile.html, but not running ITS view function.
How do i have the form submissions send a GET to the profile view while still having them send POST for their own respective functions.
Views:
def log_in(request):
if request.method == 'POST':
form = log_form(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(request, username=username, password=password)
if user is not None:
print("user exists")
login(request, user)
return render(request, 'profile.html')
else:
print("user does not exist")
print(form.errors)
return render(request, 'login.html', {
'form2': log_form
})
else:
return render(request, 'login.html', {
'form2': log_form
})
else:
return render(request, 'login.html', {
'form2': log_form
})
def profile(request, username):
if request.method == 'GET':
user = Users.objects.get(username=username)
interests = []
interest_list = Interests.objects.filter(member=user)#i got 5 objs here
for i in interest_list:
interests.append(i.item)
print('added interest')
info = Bios.objects.get(author=user)
return render(request, 'profile.html', {
'user': user,
"interests": interests,
'info': info
})
Urls:
urlpatterns = [
path('reg/', views.register, name='register'),
path('log/', views.log_in, name='login'),
path('profile/<str:username>/', views.profile, name='profile'),
path('feed/', views.feed, name='feed')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
login form:
<form method='POST' action="{%url 'login' %}">
{% csrf_token %}
<p>Username: {{form2.username}}</p>
<p>Password: {{form2.password}}</p>
{{form2.errors}}
<input type="submit" value="submit">
<p style='margin-top: 10px;'>Don't have an account?<a href="{% url 'register' %}"> Register</a></p>
</form>
register form:
<form method='POST' action="{% url 'register' %}">
{% csrf_token %}
<p>Username: {{form.username}}</p>
<p>Email: {{form.email}}</p>
<p>Password: {{form.password}}</p>
<p>Phone: {{form.phone}}</p>
<p>First name: {{form.first_name}}</p>
<p>Last name: {{form.last_name}}</p>
{{form.errors}}
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<p>
<strong>{{ error|escape }}</strong>
</p>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<p>
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
<input type='submit' value='Submit'>
<p style="margin-top: 5px;">Already have an account?<a href="{% url 'login' %}"> Log In</a></p>
</form>
Upvotes: 1
Views: 1377
Reputation: 130
The login page should be accessed twice: when the user is redirected to it , so using GET, and again when the user sends the filled form, this time using POST. Your view logic should deal with the two types of requisition:
def user_login(request):
if request.POST:
form = UserForm(request.POST) # note that I renamed your form
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect('profile', kwargs={username=user})
else: # request is GET
form = UserForm()
return render(request, 'login.html', {'form': form})
@require_GET()
def profile(request, username):
user = Users.objects.get(username=username)
interests = Interests.objects.filter(member=user).values_list('item', flat=True)
info = Bios.objects.get(author=user)
context = {
'user': user,
"interests": interests,
'info': info
}
return render(request, 'profile.html', context)
I also changed the profile view a little bit to use what I believe to be the best practices in Django function based views.
Upvotes: 1