WebFaction
Community site: login faq

Hello,

I have quite interesting problem with password protected content in Django. I can successfully protect my content or URLs in Django by using "@login_required" decorator.

Example:

Urls.py

urlpatterns = patterns('',
    url(r'^(?P<category_categoryurl>[a-zA-Z0-9_.-]+)/$', 'example.sort'),
)

Views.py

@login_required
def categorysort(request, category_categoryurl):
    latest_images_list2 = Category.objects.all().filter(categoryurl=category_categoryurl)
    return render_to_response('category.html', {'latest_images_list': latest_images_list})

So basically this example would work like this:

Dynamic urls from DB all protected by password - because of "@login_required" decorator http://www.example.com/category1/ http://www.example.com/category2/ http://www.example.com/category3/

What I want to achieve is that for example in admin I want to specify that some pages are with login and some not like in the example that with login:

http://www.example.com/category1/ http://www.example.com/category3/

Without login:

http://www.example.com/category2/

That during creation in admin specifying the url (thats easy) but also specify with checkmark do not need to login something like that.

Can I use python in the views:

If in db in Category login=True
do this:
@login_required
def categorysort(request, category_categoryurl):
    latest_images_list2 = Category.objects.all().filter(categoryurl=category_categoryurl)
    return render_to_response('category.html', {'latest_images_list': latest_images_list})

if Category login=False
do this:
def categorysort(request, category_categoryurl):
    latest_images_list2 = Category.objects.all().filter(categoryurl=category_categoryurl)
    return render_to_response('category.html', {'latest_images_list': latest_images_list})

Disregard the syntax only explaining where I am going.

Can you help me?

Thanks

asked 14 Apr '12, 13:12

rentgeeen
4721219
accept rate: 0%

edited 16 Apr '12, 17:18

johns ♦♦
5.3k212


That is basically what you would do.

You would add a Boolean value to your model that represented 'login required'.

You would than have a nested for-loop that would look something like this,

for value in model:
    if value.login_required = 1:
        # This would be where the login required logic woudld go,
    else
        # do the other stuff here
permanent link

answered 14 Apr '12, 17:03

johns ♦♦
5.3k212
accept rate: 23%

Thanks for the answer...

Would you be able to write full syntax for that?

I know it would go to models.py

loginreq = models.BooleanField()

class Page(models.Model):
    name = models.CharField(max_length=200, blank=True)
    def __unicode__(self):
        return self.name
    pageurl = models.CharField(max_length=200, unique=True)
    sort = models.CharField(max_length=200, blank=True)
    pub_date = models.DateTimeField('date published')
    loginreq = models.BooleanField()

Views.py

I want to write "if the value in that model for that record is True, find out if the user is logged in if not show him login page ELSE just show the page without login"

I know there is script for checking the user:

 if request.user.is_authenticated():
 # do something if the user is authenticated

I would like to see it to write it properly I am not advanced in python but learning :) Thanks a lot

(16 Apr '12, 16:29) rentgeeen

Submit a support ticket so I can see your live code, its hard to say what exactly you will need without seeing what objects it is going to effect in real-time.

In the support ticket I want to see the full paths to the view and model this will be in effect on and the line numbers to the relevant code, I also need the URLs to the site that these views effect. Do not just say the name of the project and general info, I will need specifics.

I can't promise I can write the full syntax, you are the developer who will be ultimately responsible for the security of your site, it is your responsibility to learn how to write the code that does that so you can know how it works. I will take a look and write as much of it as I can and answer any questions to enable you do do this yourself in the future.

(16 Apr '12, 17:10) johns ♦♦

Thanks a lot great help :)

permanent link

answered 17 Apr '12, 15:41

rentgeeen
4721219
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×901
×44
×39
×32
×1

question asked: 14 Apr '12, 13:12

question was seen: 6,741 times

last updated: 17 Apr '12, 15:41

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2016 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM