Note that there are some explanatory texts on larger screens.

plurals
  1. POMEDIA_URL and redirect not working
    primarykey
    data
    text
    <p>I'm beginner in django, so started my test project. </p> <p>There is a home.html which gives options to register or login/logoff. Also I created a separate app <code>registration</code> for handling these requests.</p> <p><strong>settings.py</strong></p> <pre><code>MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media').replace('\\','/') MEDIA_URL = 'media/' </code></pre> <p><strong>urls.py</strong></p> <pre><code>url(r'^$', direct_to_template, {"template": "home.html",}, name="home"), url(r'^register/', direct_to_template, {"template": "register.html",}, name="register"), url(r'^accounts', include('registration.urls')), </code></pre> <p><strong>base.html</strong></p> <pre><code>&lt;head&gt; &lt;link rel="stylesheet" href="{{ MEDIA_URL }}style.css" /&gt; &lt;/head&gt;&lt;body&gt; &lt;div style="text-align: right;"&gt; &lt;form action="/accounts/login" method="post"&gt; {% csrf_token %} &lt;p&gt;&lt;span&gt;Username:&lt;/span&gt;&lt;input type="text" name="username" /&gt;&lt;/p&gt; &lt;p&gt;&lt;span&gt;Password:&lt;/span&gt;&lt;input type="password" name="password"/&gt;&lt;/p&gt; &lt;p&gt;&lt;input class="submit" type="submit" value="Login" /&gt; &lt;/p&gt; &lt;/form&gt; &lt;a href="/accounts/logout"&gt;Logout&lt;/a&gt; &lt;/div&gt; &lt;h1&gt;Welcome&lt;/h1&gt; {% if user.is_authenticated %} &lt;p&gt;Welcome, {{ user.username }}. Thanks for logging in.&lt;/p&gt; {% else %} &lt;p&gt;Welcome, new user. Please log in.&lt;/p&gt; {% endif %} {% block content %} {% endblock %} </code></pre> <p><strong>home.html</strong></p> <pre><code>{% extends "base.html" %} {% block content %} &lt;p&gt; This is a test site. If you do not have an account, &lt;a href="/register"&gt;Register&lt;/a&gt; &lt;/p&gt; {% endblock %} </code></pre> <p><strong>register.html</strong></p> <pre><code>{% extends "base.html" %} {% block content %} &lt;h1&gt; Register &lt;/h1&gt; &lt;br /&gt; &lt;form action="/accounts" method="post"&gt; {% csrf_token %} &lt;p&gt;&lt;span&gt;Username:&lt;/span&gt;&lt;input type="text" name="username" /&gt;&lt;/p&gt;&lt;br /&gt; &lt;p&gt;&lt;span&gt;Email:&lt;/span&gt;&lt;input type="text" name="email" /&gt;&lt;/p&gt;&lt;br /&gt; &lt;p&gt;&lt;span&gt;Password:&lt;/span&gt;&lt;input type="password" name="password"/&gt;&lt;/p&gt;&lt;br /&gt; &lt;p&gt;&lt;span&gt;Re-password:&lt;/span&gt;&lt;input type="password" name="repassword"/&gt;&lt;/p&gt;&lt;br /&gt; &lt;p&gt;&lt;input class="submit" type="submit" value="Register" /&gt; &lt;/p&gt; &lt;/form&gt; {% endblock %} </code></pre> <p><strong>urls.py - registration</strong></p> <pre><code>url(r'^media/(?P&lt;path&gt;.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}), url(r'^$', registerfun), url(r'/login$', loginfun), url(r'/logout$', logoutfun), </code></pre> <p><strong>views.py - registration</strong></p> <pre><code>def registerfun(request): #validate and obtain request parameters if not errors: user = User.objects.create_user(username, email, password) user.save() return render_to_response('home.html',{'errors':errors}, context_instance=RequestContext(request)) def loginfun(request): #obtaion request parameters user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) return render_to_response('home.html') else: errors.append('Account disabled') return render_to_response('home.html',{'errors':errors}, context_instance=RequestContext(request)) else: errors.append('Invalid username/password') return render_to_response('home.html',{'errors':errors}, context_instance=RequestContext(request)) </code></pre> <p>I'm facing 2 problems</p> <ul> <li>css is not loading. It's under <code>media/style.css</code>.</li> <li>when logging in and logging out, the url changes from /accounts/login to /accounts/logout. How shall I avoid it and make it stay at root? Also when I try logging in when I'm on <a href="http://localhost:8000/accounts/logout" rel="nofollow">http://localhost:8000/accounts/logout</a> page it's giving me <code>CSRF verification failed. Request aborted.</code> error</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload