I would appreciate it if you could tell me how to solve these syntax errors.

Asked 2 years ago, Updated 2 years ago, 42 views

Hello, I'm a first-time Python student. It may be a very easy question, but I'm posting it here because I'm not very good at searching and understanding.

Here's the whole story of views.py I wrote.

def post_list(request):
    request.session['lat'] = request.POST['user_lat']
    request.session['lon'] = request.POST['user_lon']
    userpoint = GEOSGeometry('POINT('lat' 'lon')', srid=4326)
    list_total = list_1
    i=1
    while i:
        list_i = Post.objects.filter(point_distance__lte = userpoint, D(km=i)
        list_total = 'list_total' + ',' + 'list_i')
        result_list = list(chain(list_total))
        if len(result_list) >= 50 :
            break

    template = loader.get_template('blog/index.html')
    context = {
        'post_list': post_list,
    }

    return HttpResponse(template.render(context, request))

I'm going to use geodjango to get the lat and lon values of the user requested.POST[] as user_lat and user_lon to form coordinants and get a list accordingly.

By the way, I am trying to do python3 manage.py makeovers at the terminal

First,

    request.session['lat'] = request.POST['user_lat']
    request.session['lon'] = request.POST['user_lon']
    userpoint = GEOSGeometry('POINT('lat' 'lon')', srid=4326)

Here

userpoint = GEOSGeometry('POINT('lat' 'lon')', srid=4326)
                                  ^
                               SyntaxError: invalid syntax

with a child born

Second,

    list_i = Post.objects.filter(created_date__gt = elasped_minutes).filter(point_distance__lte = userpoint, D(km='i'))
                                                                                                                    ^
SyntaxError: positional argument follows keyword argument

This is how you get an error telling you not to write i.

And lastly,

    while i:
        list_i = Post.objects.filter(point_distance__lte = userpoint, D(km=i)
        list_total = 'list_total' + ',' + 'list_i')
        result_list = list(chain(list_total))
        if len(result_list) >= 50 :
            break

From

    list_total = list_total + ',' + 'list_i'
             ^
SyntaxError: invalid syntax

occurs.

I'm a beginner on how to solve the previous two things, so I'm hoping you'll give me a solution because I'm at a loss.

Especially the last one

    while i:
        list_i = Post.objects.filter(point_distance__lte = userpoint, D(km=i)
        list_total = 'list_total' + ',' + 'list_i')
        result_list = list(chain(list_total))
        if len(result_list) >= 50 :
            break

The list of Post models as a result of the while inquiry in

Use from itertools import chain for the result_list

I'm trying to chain it and pull it out all at once, but I don't even know if it's possible or not because all the codes I tried have errors in front of me.

I'd appreciate it if you let me know. I'm sorry for asking such a beginner question every time

Python doc for itertools: https://docs.python.org/3.3/library/itertools.html

Examples of the first and second questions: https://docs.djangoproject.com/en/dev/ref/contrib/gis/db-api/#distance-lookups I followed the example here.

Currently, I installed the latest version of Python 3.5 through apt-get, and I know that django also installed the latest version using pip3.

django python

2022-09-22 20:58

1 Answers

The part where I mixed double and single quotes seems to be wrong.

lat = request.POST['user_lat']
lon = request.POST['user_lon']

userpoint = GEOSGeometry('POINT({}, {})'.format(lat,lon) , srid=4326)

Would you like to modify it with


2022-09-22 20:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.