Sorry if you tried helping me when I asked this earlier. Had to delete that post because I thought the question was poorly written.
So I have a login view that when I click submit throws the Error:
Exception Type: IOError
Exception Value:
[Errno 22] Invalid argument: u"C:\\Users\\Me\\Desktop\\mysite\\templates\\{'user': <CustomUser: coco>}"
Traceback:
C:\Users\Me\Desktop\mysite\accounts\views.py in login
40. return render('all_posts.html', {'user': request.user}, context_instance=RequestContext(request))
Originally my login view I had:
return HttpResponseRedirect('/')
instead of the current:
return render('all_posts.html', {'user': request.user}, context_instance=RequestContext(request))
and the former sort of worked, in the since that it didn't throw any errors and directed me to the proper url. but in my template; {% if request.user.is_authenticated %} wasn't working.
So I changed it in the hopes of getting my template to display the current user's name (Welcome, coco!) , instead now I get this new error.
Here is my views.py
def login(request):
"""
Log in view
"""
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
user = authenticate(username=request.POST['username'], password=request.POST['password'])
if user is not None:
if user.is_active:
django_login(request, user)
return render('all_posts.html', {'user': request.user}, context_instance=RequestContext(request))
else:
form = AuthenticationForm()
return render_to_response('login.html', {
'authenticationform': form,
}, context_instance=RequestContext(request))
base.html
<div class="log_bar">
<ul>
{% if request.user.is_authenticated %}
<li>Welcome,</li>
<li><a href="{% url 'profile' %}">{{ user.username }}</a></li>
<li>|</li>
<li><a href="{% url 'logout' %}">Log Out</a></li>
{% else %}
<li>Please</li>
<li><a data-toggle="modal" data-target="#modal-login" href="">log in</a></li>
<li>or</li>
<li><a data-toggle="modal" data-target="#modal-register" href="">sign up</a></li>
{% endif %}
</ul>
</div><!-- /.log_bar -->
EDIT 1: Login Template login.html
{% load staticfiles %}
{% load crispy_forms_tags %}
<div class="modal" id="modal-login">
<div class="modal-dialog">
<div class="modal-content">
<form enctype="multipart/form-data" method="post" action="login/">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">Log In</h3>
</div>
<div class="modal-body">
{% csrf_token %}
{{ authenticationform|crispy }}
</div>
<div class="modal-footer">
<input type='submit' class="btn btn-primary" value="Log In" />
</div>
</form>
</div>
</div>
</div>
EDIT 2: all_posts.html
all_posts is empty right now. basically it just extends base.html. I'll post both.
all_posts.html
{% extends "base.html" %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
</html>
EDIT 3: urls.py
from django.conf.urls import url, include
from blog import views
from accounts.views import get_profile, login, logout, register
import accounts
urlpatterns = [
url(r'^textpost$', views.submit_textpost, name='textpost'),
url(r'^linkpost$', views.submit_linkpost, name='linkpost'),
url(r'^$', views.all_posts, name='all_posts'),
url(r'^all$', views.all_posts, name='all_posts'),
url(r'^new$', views.new_posts, name='new_posts'),
url(r'^all/top_hour$', views.top_posts_hour, name='top_hour'),
url(r'^all/top_day$', views.top_posts_day, name='top_day'),
url(r'^all/top_week$', views.top_posts_week, name='top_week'),
url(r'^all/top_month$', views.top_posts_month, name='top_month'),
url(r'^all/top_year$', views.top_posts_year, name='top_year'),
url(r'^all/top_beginning_of_time$', views.top_posts_all, name='top_beginning_of_time'),
url(r'u/(?P<username>[a-zA-Z0-9]+)$', accounts.views.get_profile, name='username'),
url(r'register$', register, name='register'),
url(r'^profile/(?P<username>\w+/$)', get_profile, name='profile'),
url(r'logout/$', logout, name='logout'),
url(r'^login/$', login, name='login'),
url(r'(?P<username>[a-zA-Z0-9]+)/$', views.get_sub, name='sub_url'),
url(r'(?P<username>[a-zA-Z0-9]+)/(?P<title_slug>[a-zA-Z0-9]+)/$', views.get_sub, views.title_slug, name='sub_post'),
url(r'^comments/', include('django_comments_xtd.urls')),
]
base.html full
Side Note* In my nav class="navbar navbar-fixed-left navbar-static-top" , notice all the href's are empty, that's because if I try to populate them {% url 'new_posts' %} etc.. it will throws the same Traceback:
'function' object is not iterable . I don't know if it's related, but I thought I should mention. I'm not even concerning myself with that now, I don't want to get ahead of myself. I'm just trying to learn how to do proper login for now.
{% load staticfiles %}
{% load crispy_forms_tags %}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="{% static 'css/base.css' %}">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
<TITLE>{% block title %}{% endblock %}</TITLE>
</HEAD>
<BODY>
{% block content %}
{% block navigation %}
<div class="navbar-wrapper">
<div class="post_button" style="width:58px; margin:0 auto;">
<a href="#" class="btn btn-custom blue">Submit a Post</a>
</div> <!-- /.post_button-->
<div class="log_bar">
<ul>
{% if user.is_authenticated %}
<li>Welcome,</li>
<li><a href="{% url 'profile' %}">{{ user.username }}</a></li>
<li>|</li>
<li><a href="{% url 'logout' %}">Log Out</a></li>
{% else %}
<li>Please</li>
<li><a data-toggle="modal" data-target="#modal-login" href="">log in</a></li>
<li>or</li>
<li><a data-toggle="modal" data-target="#modal-register" href="">sign up</a></li>
{% endif %}
</ul>
</div><!-- /.log_bar -->
<nav class="navbar navbar-fixed-left navbar-static-top">
<div class="container-fluid">
<!-- Collect the nav links, forms, and other content for toggling -->
<ul class="nav navbar-nav ">
<li class="active"><a href="/">Home <span class="sr-only">(current)</span></a></li>
<li><a href="">All</a></li>
<li><a href="">New</a></li
<li class="dropdown">
<a href="" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Top<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="">hour</a></li>
<li><a href="">24 hrs</a></li>
<li><a href="">week</a></li>
<li><a href="">month</a></li>
<li><a href="">year</a></li>
<li><a href="">beginning of time</a></li>
<li role="separator" class="divider"></li>
<li><a href="">Custom Search</a></li>
</ul>
</li>
</ul>
</div><!-- /.container-fluid -->
</nav>
<div id="side_bar">
<form class="navbar-form navbar-static-top navbar-right" role="search" id="navBarSearchForm">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search">
<span class="input-group-btn">
<button type="submit" class="btn btn-default" id="search_btn">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</form>
</div><!-- /.side-bar -->
<button class="btn-block" id='hideshow' value='hide/show' style="display: block; height: 100%;"></button>
{% include 'register.html' %}
{% include 'login.html' %}
{% endblock %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"> </script>
<script type="text/javascript" src="{{ STATIC_URL }} /static/jquery.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }} /static/jquery.leanModal.js"></script>
{% endblock %}
</BODY>
</HTML>
render的第一个参数必须是request object
,第二个模板名称和第三个上下文。
return render(request, 'all_posts.html', {'user': request.user})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.