Pregunta

I am not even sure what category in Django this question falls in and I am very new to django. I have tried looking for Django post requests, parameter passing and even checked under Django APIs but have not found what I am looking for. What I am trying to do is create an API for my client but it must be done in Django. If I was to do this in .Net I could use http post and http get and web services but I am not at all sure how this is done in Django. What my client wants to do is to be able to see:

  1. Enter username and password in url with parameters for date and id and be able to view rooms available based on what is entered
  2. Enter username, password and dates and room code to be able to book the room

No interface needed just simple parameter passing through url. Is this possible with Django and if yes can somebody please point me in the right direction.

¿Fue útil?

Solución

What you're looking for are captured parameters

Below is a code snippet from the above link.

# urls.py
from django.conf.urls import patterns, url

urlpatterns = patterns('blog.views',
    url(r'^blog/(?P<year>\d{4})/$', 'year_archive', {'foo': 'bar'}),
)

# views.py
def year_archive(request, year, foo=None):
    # view method definition

Otros consejos

As of Django 1.10, patterns was removed from Django.

Here is a 2019 Django 2.2 solution.

It looks like re_path is the current recommended tool for capturing parameters via regular expressions:
https://docs.djangoproject.com/en/2.2/ref/urls/#re-path

# urls.py
from django.urls import re_path

from myapp import views

urlpatterns = [
    re_path(r'^blog/(?P<year>\d{4})/$', 'year_archive', {'foo': 'bar'}),
]

# views.py
def year_archive(request, year, foo=None):
    # view method definition
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top