1

I want to add to my Django project (using Django rest framework ) new get function. I want it to just send to me in Json "Hello world" without writing or without opening a table in the DB this is my code:

@api_view()
def about(request):
    return Response({"message": "Hello, world!"})

i put it in view.py what more i should do the make it work? when i run the server and write this lines

http://127.0.0.1:8000/about

i want it to saw me "{"message": "Hello, world!"}"

1
  • And what you've got instead? Commented Dec 29, 2014 at 7:54

3 Answers 3

2

Update your 'urls.py':

urlpatterns = patterns('',
    ...
    url(r'^about/$', views.about),
    ...
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! if i want to send parameters to my function how i can do it?
Tutorials and docs are your friends. docs.djangoproject.com/en/1.7/intro/tutorial03
1

I think you should register url path to urls.py first. And then update settings.py.

Here are some instructions for you about Django rest framework quickstart. http://www.django-rest-framework.org/tutorial/quickstart/

1 Comment

yeah i thought so but i didn't know how
-2

To send parameters you can added them in the URL and ask for request.GET inside your function, even you can make a POST and ask for request.data inside your function.

your url: /about/?start_date=2016/08/20&end_date=2016/08/21

def about(self, request, *args, **kwargs):
    """"
    Is called by a GET method.
    ISO format: YYYY/MM/DD and in UTC.
    """

    start_date_str = request.GET.get("start_date")
    end_date_str = request.GET.get("end_date")

    return Response({"message": "Hello, world!"})

Another way:

Your url: /about/100/

urlpatterns = patterns('',
...
url(r'^about/(?P<object_pk>[0-9]+)/$', views.example),
...

def example(self, request, object_pk, *args, **kwargs):
    """"
    Is called by a GET method.
    """
    # do something with your object

    return Response({"message": "Hello, world!"})

1 Comment

This answer is not clear at all, please specify more details regarding what you are trying to say.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.