Sunday, March 29, 2020

Django Login Problem

{% csrf_token %} Enter a username:
Enter a Password:

Signup Complete!

Return to message board
{% for post in posts %}
{{post.content}}
Post By: {{post.author.username}}
{% if post in user.post_set.all %}
{% csrf_token %}
{% endif %}
{% endfor %} {% if user_login == True %} Add New Post {% endif %}
{% for topic in topics %}
{{topic.title}} View Posts
{% endfor %} {% if user_login == True %} Create New Topic {% endif %} from django.conf.urls import url
from . import views


urlpatterns = [


url(r'^signin/$', views.sign_in, name = "signin"),
url(r'^$', views.index, name = "main"),
url(r'^logout/$', views.logout_view, name = "logout"),
url(r'^new_topic/$', views.new_topic, name = "new-topic"),
url(r'topic/(?P<topic_id>\d+)/$', views.topic_detail, name = "topic-detail"),
url(r'^add_post/$', views.add_post, name ="add-post"),
url(r'^delete_post/$', views.delete_post, name = "delete-post"),
url(r'^signup/$', views.signup, name = "sign-up"),
]from django.shortcuts import render, redirect
from .models import Topic, Post
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User

# Create your views here.
def add_post(request):
post_content = request.POST['post_content']
post_topic = Topic.objects.get(id=request.POST['topic_id'])
newPost = Post()
newPost.author = requset.user
newPost.content = post_content
newPost.topic = post_topic
newPost.save()
return redirect('topic-detail',topic_id = post_topic.id)
def signup(request):
if request.method =="POST":
username = request.POST['username']
password = request.POST["password"]
newUser = User()
newUser.username = username
newUser.set_password(password)
newUser.save()
return render(request, 'signup-complete.html',{})
return render(request,'signup.html',{})
def delete_post(request):
post_id = request.POST['post_id']
delete_post_obj = Post.objects.get(id=post_id)
topic_id = delete_post_obj.topic.id
delete_post_obj.delete()
return redirect('topic-detail', topic_id=topic_id)
def check_user_auth(user):
if user.is_authenticated:
return True
else:
return False
def topic_detail(request, topic_id=None):
topic=Topic.objects.get(id=topic_id)
posts = topic.post_set.all()
ctx = {'topic':topic, 'user_login' :check_user_auth(request.user), 'posts':posts}
return render(request, 'topic-detail.html', ctx)
def new_topic(request):
topic_name = request.POST['topic_name']
newTopic = Topic()
newTopic.title = topic_name
newTopic.save()
return redirect('main')
def logout_view(request):
logout(request)
return redirect("main")

def sign_in(request):
#if request.method == 'POST':
#form = AuthenticationForm(request=request, data=request.POST)
#if form.is_valid():
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request,username=username, password=password)
if user is not None:
login(request,user)
return redirect('main')
#else:
#message.error(request, "Invalid username or password.")
#else:
#message.error(request, "Invalid username or password.")


def index(request):
topics = Topic.objects.all()
ctx = {'topics':topics,'user_login':check_user_auth(request.user)}
return render(request, 'topic-list.html', ctx)
"""MessageBoard URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from board import urls as appurls
from django.contrib import admin
admin.autodiscover()

urlpatterns = [
url(r'^',include(appurls)),
url(r'^admin/', admin.site.urls),
]
I have problem in loging in after signing up. I have tried all the methods that I can try, I want to know my errors.
Thank you

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f542af27-682f-4528-b1e8-70acfc6c8e1e%40googlegroups.com.

No comments:

Post a Comment