Login view works! Forms have been reshuffled (Django apparently comes with built...
authorKriti Godey <kriti.godey@gmail.com>
Tue, 2 Mar 2010 21:48:36 +0000 (16:48 -0500)
committerKriti Godey <kriti.godey@gmail.com>
Tue, 2 Mar 2010 21:48:36 +0000 (16:48 -0500)
forms.py
templates/allsandwiches.html
templates/login.html [new file with mode: 0644]
templates/onesandwich.html
urls.py
views.py

index e7d2728..cd81af2 100644 (file)
--- a/forms.py
+++ b/forms.py
@@ -1,6 +1,7 @@
 from django.forms import ModelForm
 from models import Sandwich, Ingredient
 from django.contrib.auth.models import User
 from django.forms import ModelForm
 from models import Sandwich, Ingredient
 from django.contrib.auth.models import User
+from django import forms
 
 
 class SandwichForm(ModelForm):
 
 
 class SandwichForm(ModelForm):
@@ -14,18 +15,4 @@ class IngredientForm(ModelForm):
 
        class Meta:
                model = Ingredient
 
        class Meta:
                model = Ingredient
-               exclude = ('slug',)
-
-
-class UserLoginForm(ModelForm):
-       
-       class Meta:
-               model = User
-               fields = ('username', 'password')
-       
-class NewAccountForm(ModelForm):
-       
-       class Meta:
-               model = User
-               fields = ('username', 'first_name', 'last_name', 'email', 'password')
-               
\ No newline at end of file
+               exclude = ('slug',)
\ No newline at end of file
index 64f734f..102f86c 100644 (file)
@@ -6,7 +6,7 @@
                {% for s in sandwiches %}
                        <h3>{{ s.adjective }}</h3>
                        <p>Made on {{ s.date_made }} and added by {{ s.user.username }}.</p>
                {% for s in sandwiches %}
                        <h3>{{ s.adjective }}</h3>
                        <p>Made on {{ s.date_made }} and added by {{ s.user.username }}.</p>
-                       <img src="{{ s.picture }}">
+                       <img src="{{ s.picture.url }}">
                        <h5>Ingredients</h5>
                        <ul>
                                {% for i in s.ingredients.all %}
                        <h5>Ingredients</h5>
                        <ul>
                                {% for i in s.ingredients.all %}
diff --git a/templates/login.html b/templates/login.html
new file mode 100644 (file)
index 0000000..367b112
--- /dev/null
@@ -0,0 +1,11 @@
+<html>
+       <head>
+               <title>Login</title></head>
+       <body>
+               <h1>Login</h1>
+               <form enctype="multipart/form-data" action="/login/" method="post">
+               {{ aform.as_p }}
+               <input type="submit" value="Submit" />
+               </form>
+       </body>
+</html>
\ No newline at end of file
index 0feaa5b..a8096db 100644 (file)
@@ -4,7 +4,7 @@
        <body>
                <h3>{{ s.adjective }}</h3>
                <p>Made on {{ s.date_made }} and added by {{ s.user.username }}.</p>
        <body>
                <h3>{{ s.adjective }}</h3>
                <p>Made on {{ s.date_made }} and added by {{ s.user.username }}.</p>
-               <img src="{{ s.picture }}">
+               <img src="{{ s.picture.url }}">
                <h5>Ingredients</h5>
                <ul>
                        {% for i in s.ingredients.all %}
                <h5>Ingredients</h5>
                <ul>
                        {% for i in s.ingredients.all %}
diff --git a/urls.py b/urls.py
index a2025b8..dd6fede 100644 (file)
--- a/urls.py
+++ b/urls.py
@@ -4,6 +4,7 @@ from django.shortcuts import render_to_response
 
 urlpatterns = patterns('',
        (r'^addsandwich/$', views.add_sandwich),
 
 urlpatterns = patterns('',
        (r'^addsandwich/$', views.add_sandwich),
+       (r'^login/$', views.login_view),
        (r'^addingredient/$', views.add_ingredient),
        (r'^allsandwiches/$', views.all_sandwich),
        (r'^newsandwiches/$', views.newsandwiches),
        (r'^addingredient/$', views.add_ingredient),
        (r'^allsandwiches/$', views.all_sandwich),
        (r'^newsandwiches/$', views.newsandwiches),
index dff7b66..872104f 100644 (file)
--- a/views.py
+++ b/views.py
@@ -4,6 +4,8 @@ from django.shortcuts import render_to_response
 from django.core.files.uploadedfile import SimpleUploadedFile
 from models import Sandwich, Ingredient
 from django.http import Http404
 from django.core.files.uploadedfile import SimpleUploadedFile
 from models import Sandwich, Ingredient
 from django.http import Http404
+from django.contrib.auth import authenticate, login
+from django.contrib.auth.forms import AuthenticationForm
 import datetime
 
 
 import datetime
 
 
@@ -59,4 +61,25 @@ def specific_sandwich(request, slug):
                sandwiches = Sandwich.objects.get(slug=slug)
        except Sandwich.DoesNotExist:
                raise Http404
                sandwiches = Sandwich.objects.get(slug=slug)
        except Sandwich.DoesNotExist:
                raise Http404
-       return render_to_response('onesandwich.html', {'s': sandwiches,})
\ No newline at end of file
+       return render_to_response('onesandwich.html', {'s': sandwiches,})
+
+
+def login_view(request):
+       try:
+               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)
+                               thankshtml = "<p class=\"formthanks\">You have been logged in</p>"
+                               return HttpResponse(thankshtml)
+                       else:
+                               thankshtml = "<p class=\"formthanks\">Disabled account. Sorry.</p>"
+                               return HttpResponse(thankshtml)
+               else:
+                       thankshtml = "<p class=\"formthanks\">Invalid login!</p>"
+                       return HttpResponse(thankshtml)
+       except KeyError:
+               aform = AuthenticationForm()
+               return render_to_response('login.html', {'aform': aform,})
\ No newline at end of file