[Answer]-Django running wrong view method

1👍

I don’t know exactly why /twitter/ view is called, but I can see two things to change:

  1. You should use a string as the second parameter for url(), as you can see in this example [1]. You can use ‘myapp.views.my_method’ nomenclature.
  2. You forgot to start the request URL with ^ that indicates the start of the URL.

About the unbounded code, I don’t know if that could be causing the problem. But I can’t see why are you putting that code unbounded. I am not sure when that code would be executed, I guess the first time you call a view in that file and Django loads the file (I’m guessing, I don’t know exactly), but I don’t think that would be a good way to do that. Think when do you want to execute that code, put it in a method, and call it.

[1] https://docs.djangoproject.com/en/1.5/topics/http/urls/#example

0👍

HI hemant i am wondering why you have written request_view.py.

Please see the django docs.

what you can do is .

Create two function in your views.py like

def answer(request):

   do some stuffs.

   render_to_response(template.html)

and on the same page write another

def display_meta(request):

   # do your studd
   render_to_response(some.html)

YOU NEED NOT TO CREATE TWO SEPERATE VIEWS.PY

I dont know what this code does

l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
keyword = input('enter the keyword you want to search for?')
stream.filter(track = [keyword])

But if you want to use StdOutListener inside your function you can call in your view

like

def display_meta(request):
   stobject  = StdOutListener()  # use parameters if you have here 
   # do your studd
   render_to_response(some.html)

0👍

Organizing views into a python package could solve this problem. So if you have a structure like this…

# views.py
def SomeView(request):
    return HttpResponse('SomeView')

# another_view.py
def AnotherView(request):
    return HttpResponse('AnotherView')

Your can reorganize these separate view files into a views package. That is…

# views
    # __init__.py
    from views import SomeView
    from another_view import AnotherView

    # views.py
    def SomeView(request):
        return HttpResponse('SomeView')

    # another_view.py
    def AnotherView(request):
        return HttpResponse('AnotherView')

And now, everything can be called in a django-standard way:

url(r'^url-to-some-view/$', 'views.SomeView'),
url(r'^url-to-another-view/$', 'views.AnotherView'),

UPDATED:

To make a ‘python package’…

  1. Create a views directory at the same level as the view.py file [mkdir views]
  2. Create a __init.py__ file inside the views directory # this is what makes a directory a ‘python package’
  3. Move views.py into the views directory.
  4. Move your request_view.py into the views directory.
  5. Edit the __init__.py file with the necessary import statements. In this case:

    from views import answer
    from request_view import display_meta
    

What this does is replace a file with a directory. By importing everything into the __init__.py file, this directory looks like a large file to your code, rather than another module.

👤Cole

Leave a comment