我是第一次尝试使用Django应用程序,我想从URL中提取参数并使用它们来更新Django应用程序中的数据库。该应用程序的一个页面包含一个jQuery脚本,该脚本发送包含用户输入信息http://127.0.0.1:8000/storelatlong/?lng=56.237244700410336&lat=-4.6197509765625
和lat
参数的lng
形式的ajax GET请求。我想检索该信息并使用它来更新我的数据库中存储的UserProfile.latitude
和UserProfile.longitude
值。
所以,我有两个基本问题:
如何将参数信息转换为views.storelatlong
?
一旦有信息,如何使用它来更新models.UserProfile
?
urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from moddle import views
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^about/$', views.about, name='about'),
url(r'^contact_us/$', views.contact_us, name='contact_us'),
url(r'^faq/$', views.faq, name='faq'),
url(r'^search/$', views.search, name='search'),
url(r'^register/$', views.register, name='register'),
url(r'^login/$', views.user_login, name='login'),
url(r'^logout/$', views.user_logout, name='logout'),
url(r'^$', views.index, name='index'),
url(r'^(?P<username>[\w\-]+)/$', views.user_profile, name='user_profile'),
url(r'^(?P<username>[\w\-]+)/mybookings/$', views.view_bookings, name='view_bookings'),
url(r'^(?P<username>[\w\-]+)/addbike/$', views.upload_bike, name='upload_bike'),
url(r'^(?P<bike_id_slug>[\w\-]+)/$', views.bike_profile, name='bike_profile'),
url(r'^(?P<bike_id_slug>[\w\-]+)/request/$', views.request_bike, name='request_bike'),
url(r'^storelatlong/$', views.storelatlong, name='storelatlong'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py
def user_profile(request, username):
context_dict = {}
try:
user = User.objects.get(username=username)
userprofile = UserProfile.objects.get(user=user)
context_dict['userprofile'] = userprofile
users_bikes = Bike.objects.filter(owner=userprofile)
context_dict['bikes'] = users_bikes
except UserProfile.DoesNotExist:
print "User does not exist"
context_dict['bikes'] = None
context_dict['userprofile'] = None
return render(request, 'MyApp/user_profile.html', context=context_dict)
def storelatlong(request):
context_dict = {'': ''}
lat = float(request.GET.get('lat', ''))
lng = float(request.GET.get('lng', ''))
return HttpResponse("OK")
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
phone_number = models.CharField(max_length=16, blank=True)
gender_male = models.BooleanField()
post_code = models.CharField(max_length=7)
longitude = models.FloatField(null=True, blank=True)
latitude = models.FloatField(null=True, blank=True)
def __str__(self):
return self.user.username
def __unicode__(self):
return self.user.username
目前,发送GET请求只会导致我在浏览器和命令行服务器中获得301(永久移动)和500(内部服务器错误)消息:
[10/Mar/2017 15:43:28] "GET /storelatlong lng=56.359671608143785&lat=-5.23773193
359375 HTTP/1.1" 301 0
Internal Server Error: /storelatlong/
Traceback (most recent call last):
File "H:\MyAppEnv\lib\site-packages\django\core\handlers\exception.py", line 39, in inner response = get_response(request)
File "H:\MyAppEnv\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "H:\MyAppEnv\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "H:\MyApp\MyApp_project\MyApp\views.py", line 33, in user_profile user = User.objects.get(username=username)
File "H:\MyAppEnv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "H:\MyAppEnv\lib\site-packages\django\db\models\query.py", line 385, in get self.model._meta.object_name
DoesNotExist: User matching query does not exist.
[10/Mar/2017 15:43:28] "GET /storelatlong/lng=56.359671608143785&lat=-5.23773193359375 HTTP/1.1" 500 18754
提前感谢您的时间和智慧。
答案 0 :(得分:1)
Django按顺序处理URL模式,直到找到匹配的模式。不幸的是,您的user_profile
有一个匹配任何字母数字字符串的模式 - 包括&#34; storelatlong&#34;。因此,正在调用user_profile视图,并且它尝试检索用户&#34; storelatlong&#34;,这不存在 - 因此错误。
您可以通过将storelatlong
模式移到user_profile上面来解决此问题。但是,对于bike_profile
视图,您将遇到完全相同的问题 - 这与user_profile
具有完全相同的模式,因此Django无法区分它们。您应该使用前缀或后缀来使bike_profile模式唯一。