Sunday, November 30, 2014

Re: i want to know how to use SlugField with slugify to generate url for detail_blog page without using get_absolute_url

Hi,

Is this what you're looking for?

{% url 'add_view' quotation.slug %}

Collin


On Thursday, November 27, 2014 4:26:49 PM UTC-5, Kanchan Prasad wrote:
my model.py is
class BlogPost(models.Model):
    title        = models.CharField(max_length=100)
    text         = models.TextField()
    created_on   = models.DateTimeField(auto_now_add=True,auto_now=False)
    updated_on   = models.DateTimeField(auto_now_add=False,auto_now=True)
    submitted_by = models.ForeignKey(User)
    slug         = models.SlugField(max_length=100,unique=True)
    def __str__(self):
        return self.title
    def save(self, *args, **kwargs):
        if not self.id:
            self.slug = slugify(self.title)
            super(BlogPost,self).save(*args,**kwargs)

urls.py

url(r'^profile/latest-quotation/(?P<slug>[\w-]+)/$',add_view, name='add_view'),

views.py
def add_view(request,slug):
    blog = get_object_or_404(BlogPost, slug=slug)
    com_form = CommentForm(request.POST or None)
    if com_form.is_valid():
        form = com_form.save(commit=False)
        form.blogpost = blog
        form.name = request.user
        form.save()
        return HttpResponseRedirect(reverse('socialnetwrok:add_view'))
    return render(request,'socialnetwork/detailview.html',{'blog':blog,'com_form':com_form})

my template where i am using it
        {% for quotation in latest_quotation_list %}
            <li><h2>
        <a href="{% url 'socialnetwork:add_view' title.slug %}">{{quotation.title}}</a>
            </h2>    </li>
        <p>{{quotation.text|truncatewords:100}}</p>

please help me i need help

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d334838e-5b9f-4d2f-9b98-75c5a08265ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Simple Login Problem

Hi,

If you type your password wrong, user will be None, and your code will then redirect to the "register" page.

If you press "back" after a successful login, the CSRF token will be out of date and the form won't work.

Are you caching any pages?

Collin

On Friday, November 28, 2014 12:55:35 AM UTC-5, Rootz wrote:
I have a django app but I having problems with my login views and logout views. I do not have a html template designated to handle user login/logout view.
Django project is configured as follows:

INSTALLED_APPS setting:

  1. 'django.contrib.auth' contains the core of the authentication framework, and its default models.
  2. 'django.contrib.contenttypes' is the Django content type system, which allows permissions to be associated with models you create.
  3. 'django.contrib.sessions',

 MIDDLEWARE_CLASSES setting:

  1. SessionMiddleware manages sessions across requests.
  2. AuthenticationMiddleware associates users with requests using sessions.
  3. csrf.CsrfViewMiddleware 

Using Django Template Language and Template inheritance. The login form is on the base template on other templates extends from this base template.

All my login attempts result in some of the views rendering the user info (username to welcome user back) while other views rendering the page as if the user is an anonymous user. If I try to login in again I get an error page stating that there is a missing csrf token or incorrect. Adding to this I have identified many instances where I have tried to logout and it does not seem to log me out because it is still showing the last user login info. For my base template I have hard coded the form (meaning not using Django Form class).

Can You identify the possible fault in how i am implementing the login and logout views?

 
 Here is a copy of my login and logout views

def members_login(request):

    if request.method == 'POST':
        password = request.POST['password']
        username = request.POST['username']
        user = authenticate(username=username,password=password)

        if user is not None:
            if user.is_active:
                login(request,user)
                return redirect('members:index')
            else:
                #inactive users required to re-register
                return redirect('members:index')
        else:
            #no account required to register to create one
            return redirect('members:register')
    
    else:
        #test if login is a regular get request then redirect
        return HttpResponseRedirect(reverse('members:index'))


def members_logout(request):
    logout(request)
    return redirect('members:index')

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c106f934-66a0-4f60-b1b4-05095b9adf73%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

simplifying double decorators?

Hi,

I'm running into the situation where I have several views with the same set of decorators:

@login_required()
@user_passes_test(some_test_function, login_url='/', redirect_field_name=None)
def some_view(request):
    # some code
    return render(request, 'some_template.html', locals())

How would I go about combining the two (or more) decorators into a single decorator that can be used instead and retain the functionality?

IE: this:

@login_required()
@user_passes_test(some_test_function, login_url='/', redirect_field_name=None)
def some_view...

becomes abstracted to something like this:

@my_custom_decorator_with_redirects()
def some_view...


--
    R.

Richard Brockie

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKv-vOU6Q52yjd%3DH%2B%2B7XbvQmzswpa7iipJKYW%3DjPSh74YRC_5g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: Newbie help - running django-admin.py produces empty mysite folder

Hi,

Are there any error messages?

Is there already an existing mysite folder?

What method did you use to install django? Did you really install django 1.8, not 1.7?

Collin


On Thursday, November 27, 2014 10:15:51 AM UTC-5, David Pride wrote:
Title says it all really. Have just installed Django 1.8, can test django version and get correct response so am *fairly* happy it's installed correctly. I have had Python 2.7 installed and been using perfectly for several months.

Altered the PATH variable as discussed so it points to correct directory.

However when I run...

django-admin.py startproject mysite

The mysite folder is created - but it is empty, none of the following is created...

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

All tips gratefully received!

Many thanks,

D.P.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ed43c9f1-5cde-4d3b-baba-383f65bb702b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Newbie: How to implement a app/module, which I can include from my html?

Assignment tags are my favorite.

@register.assignment_tag
def get_categories():
   
return Category.objects.all()

{% get_categories as categories %}
{% for category in categories %}
{{ category }}
{% endfor %}

Collin


On Thursday, November 27, 2014 5:19:20 AM UTC-5, somecallitblues wrote:

https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags here you go chief

On 27/11/2014 8:04 pm, "ThomasTheDjangoFan" <stefan.eich...@googlemail.com> wrote:
Oh ok.

so it would be something like:

in home.html
{% include 'category-list.html' %}

in category-lists.html
{% load those_categories %}
<!-- display the categories as html -->

?

Am Donnerstag, 27. November 2014 09:55:50 UTC+1 schrieb James Bennett:
The usual way would be to write a custom template tag that fetches the objects and puts them into the template context.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d88ef650-0d18-4d6d-bad9-8a6a990524bf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9690882c-49da-4ee6-a01b-77270fbc7585%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Problem implementing django-sslserver

Hi,

A few things.

The stacktrace looks like it's getting gibberish. Maybe your cert and key don't match?

When I try, I can't get it to listen on a different port number. (It looks like you're trying to have it listen on 8020)

Why not have apache (or nginx) handle the SSL for you?

Collin


On Wednesday, November 26, 2014 1:07:52 PM UTC-5, pythonista wrote:


The application was working successfully on the Linux server.

I am trying to implement the ssl functionality and getting the  stack trace below after following the instructions

Any suggestions as to what is going on and how to fix this.

We are running the dev server with a separate Apache server acting as a proxy.

The comnmand line is

$ python manage.py runsslserver --certificate /path/to/certificate.crt --key /path/to/key.key  ip_adress:8020

Thanks

 

[26/Nov/2014 10:38:24] "GET /waitlist/ HTTP/1.1" 200 6341
[26/Nov/2014 11:30:50] code 400, message Bad HTTP/0.9 request type ('\x16\x03\x01\x00C\x01\x00\x00?\x03\x01Tv\x0eJ\xb2\xecd\x1f\xf5)\xd3\x8a\x9a4\xa5\x8c\xd3U;\x04C')
Traceback (most recent call last):
  File "/opt/python-2.7.8/lib/python2.7/SocketServer.py", line 595, in process_request_thread
    self.finish_request(request, client_address)
  File "/opt/python-2.7.8/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/opt/python-2.7.8/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/core/servers/basehttp.py", line 129, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "/opt/python-2.7.8/lib/python2.7/SocketServer.py", line 651, in __init__
    self.handle()
  File "/opt/python-2.7.8/lib/python2.7/wsgiref/simple_server.py", line 117, in handle
    if not self.parse_request(): # An error code has been sent, just exit
  File "/opt/python-2.7.8/lib/python2.7/BaseHTTPServer.py", line 281, in parse_request
    "Bad HTTP/0.9 request type (%r)" % command)
  File "/opt/python-2.7.8/lib/python2.7/BaseHTTPServer.py", line 368, in send_error
    self.send_response(code, message)
  File "/opt/python-2.7.8/lib/python2.7/BaseHTTPServer.py", line 385, in send_response
    self.log_request(code)
  File "/opt/python-2.7.8/lib/python2.7/BaseHTTPServer.py", line 422, in log_request
    self.requestline, str(code), str(size))
  File "/opt/python-2.7.8/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/core/servers/basehttp.py", line 136, in log_message
    msg = "[%s] %s\n" % (self.log_date_time_string(), format % args)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb2 in position 16: ordinal not in range(128)


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6d0aa6d6-c5d8-4dac-95d1-550509bbdae2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Python: Assign variable in if statement?


On 2014-11-30, at 12:30 , ThomasTheDjangoFan <stefan.eichholz.berlin@googlemail.com> wrote:

Hi guys,

coming from php I am wondering if there is a way to do something like this in Python/Django:

if variable = get_a_value_from_function():
  new_stuff = variable

Of course I can use

variable = get_a_value_from_function()
if variable:
  new_stuff = variable

But is there a shortcut similar to php?

Not as such. Python intentionally didn't include assignment within
statements (mostly conditionals) to avoid the common issue of
assignments-instead-of-equality bugs.

If `new_stuff` has a default value, you could always write.

    new_stuff = some_function() or new_stuff

which will reassign `new_stuff` to itself if `some_function()` returns a
falsy value. If you need a more complex conditional body than just an
assignment, you'll need the long one.