Django View Questions

112 views
Skip to first unread message

G Z

unread,
Jun 19, 2014, 1:02:21 PM6/19/14
to django...@googlegroups.com
so I have a problem when ever I go to /manage/ it takes me to index and im not sure why.

Urls.py
url(r'^', views.IndexView.as_view(), name='index'),
url(r'^manage/$', views.ManageView.as_view(), name='manage'),



Views.py

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
from django.views import generic
from portal.models import Customer
from django.views.generic import TemplateView

class IndexView(generic.ListView):
      model = Customer
      template_name = 'index.html'
      context_object_name = 'Customer'

class ManageView(TemplateView):
  
      template_name = 'manage.html'
      context_object_name = 'form'
      
     @login_required(login_url='/manage/')
      def my_view(request):
         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('index.html' % request.path)         
                
            else:
              return HttpResponse("Your, Account is Disabled.")
         
      def logout_view(request):
         logout(request)


manage.html

{% block content %}

{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}

<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<table>
<tr>
    <td>{{ form.username.label_tag }}</td>
    <td>{{ form.username }}</td>
</tr>
<tr>
    <td>{{ form.password.label_tag }}</td>
    <td>{{ form.password }}</td>
</tr>
</table>

<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>

{% endblock %}



index.html
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'customer/style.css' %}" />
{% if Customer %}
    <ul>
    {% for Customer in Customer %}
      
       <li>{{ Customer.customer_name }}</li>
    {% endfor %}
    </ul>
{% else %}
    <p>No customers are available.</p>
{% endif %}

G Z

unread,
Jun 19, 2014, 1:08:05 PM6/19/14
to django...@googlegroups.com
if i comment out the urls for #url(r'^$', views.IndexView.as_view(), name='index'), it will give me an error that it cant find /manage


Page not found (404)

Request Method:GET
Request URL:http://127.0.0.1:8000/

Using the URLconf defined in holon.urls, Django tried these URL patterns, in this order:

  1. ^$ ^manage/$ [name='manage']
  2. ^portal/
  3. ^manage/$
  4. ^admin/

The current URL, , didn't match any of these.

C. Kirby

unread,
Jun 19, 2014, 1:10:56 PM6/19/14
to django...@googlegroups.com
You actually have a urls error.

Your first one: url(r'^', views.IndexView.as_view(), name='index'),
Should be: url(r'^$', views.IndexView.as_view(), name='index'),  (Note the $)

$ tells the url resolver to stop trying to match a url at the $. Since you don't have it, calls to manage/ match on the first url and never go through to match the second.

Kirby

G Z

unread,
Jun 19, 2014, 1:30:02 PM6/19/14
to django...@googlegroups.com
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^register/$', views.register, name='register'), # ADD NEW PATTERN!

is my new urls



from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
from django.views import generic
from portal.models import Customer
from django.views.generic import TemplateView
from portal.forms import UserForm, UserProfileForm

class IndexView(generic.ListView):
      model = Customer
      template_name = 'index.html'
      context_object_name = 'Customer'
      
def register(request):
    # Like before, get the request's context.
    context = RequestContext(request)

    # A boolean value for telling the template whether the registration was successful.
    # Set to False initially. Code changes value to True when registration succeeds.
    registered = False

    # If it's a HTTP POST, we're interested in processing form data.
    if request.method == 'POST':
        # Attempt to grab information from the raw form information.
        # Note that we make use of both UserForm and UserProfileForm.
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        # If the two forms are valid...
        if user_form.is_valid() and profile_form.is_valid():
            # Save the user's form data to the database.
            user = user_form.save()

            # Now we hash the password with the set_password method.
            # Once hashed, we can update the user object.
            user.set_password(user.password)
            user.save()

            # Now sort out the UserProfile instance.
            # Since we need to set the user attribute ourselves, we set commit=False.
            # This delays saving the model until we're ready to avoid integrity problems.
            profile = profile_form.save(commit=False)
            profile.user = user

            # Did the user provide a profile picture?
            # If so, we need to get it from the input form and put it in the UserProfile model.
            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']

            # Now we save the UserProfile model instance.
            profile.save()

            # Update our variable to tell the template registration was successful.
            registered = True

        # Invalid form or forms - mistakes or something else?
        # Print problems to the terminal.
        # They'll also be shown to the user.
        else:
            print user_form.errors, profile_form.errors

    # Not a HTTP POST, so we render our form using two ModelForm instances.
    # These forms will be blank, ready for user input.
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    # Render the template depending on the context.
    return render_to_response(
            'register.html',
            {'user_form': user_form, 'profile_form': profile_form, 'registered': registered},
            context)


is my new views

it still can't find the register page if i comment out the index page, if i dont comment out he index page it defaults to it because it cant find the register page.

G Z

unread,
Jun 19, 2014, 1:45:46 PM6/19/14
to django...@googlegroups.com
I chagned it further and it is still not working 



from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
from django.views import generic
from portal.models import Customer
from portal.forms import UserForm, UserProfileForm
def user_login(request):
    # Like before, obtain the context for the user's request.
    context = RequestContext(request)

    # If the request is a HTTP POST, try to pull out the relevant information.
    if request.method == 'POST':
        # Gather the username and password provided by the user.
        # This information is obtained from the login form.
        username = request.POST['username']
        password = request.POST['password']

        # Use Django's machinery to attempt to see if the username/password
        # combination is valid - a User object is returned if it is.
        user = authenticate(username=username, password=password)

        # If we have a User object, the details are correct.
        # If None (Python's way of representing the absence of a value), no user
        # with matching credentials was found.
        if user:
            # Is the account active? It could have been disabled.
            if user.is_active:
                # If the account is valid and active, we can log the user in.
                # We'll send the user back to the homepage.
                login(request, user)
                return HttpResponseRedirect('portal.html')
            else:
                # An inactive account was used - no logging in!
                return HttpResponse("Your Rango account is disabled.")
        else:
            # Bad login details were provided. So we can't log the user in.
            print "Invalid login details: {0}, {1}".format(username, password)
            return HttpResponse("Invalid login details supplied.")

    # The request is not a HTTP POST, so display the login form.
    # This scenario would most likely be a HTTP GET.
    else:
        # No context variables to pass to the template system, hence the
        # blank dictionary object...
        return render_to_response('login.html', {}, context)
     
def index(request):
    welcome = 'Welcome to Enki-Corp!'
    return render(request, 'index.html', {'welcome': welcome})




app urls.py

from django.conf.urls import patterns, url
from portal import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    #url(r'^about/$', views.about, name='about'),
    url(r'^register/$', views.register, name='register'),
    url(r'^login/$', views.user_login, name='login'),
)



project urls


from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'holon.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^$', include('portal.urls')), 
    url(r'^register/', include('portal.urls')),
    url(r'^login/', include('portal.urls')),
    url(r'^admin/', include(admin.site.urls)),
   
)

C. Kirby

unread,
Jun 19, 2014, 1:52:19 PM6/19/14
to django...@googlegroups.com
Do you have APPEND_SLASH set in settings? If you don't and are trying to hit register instead of register/ it will fail

Kirby

C. Kirby

unread,
Jun 19, 2014, 1:52:49 PM6/19/14
to django...@googlegroups.com
According to the error, you didn't try to access /manage, just the root url

G Z

unread,
Jun 19, 2014, 3:20:19 PM6/19/14
to django...@googlegroups.com
thansk I solved it..
Reply all
Reply to author
Forward
0 new messages