NameError: name doohickey is not defined - python

I am having issues importing a function from a module I created into my code. I am getting an error stating that the function is not defined, despite importing the module into my file.
The error message:
something = doohickey()
NameError: name 'doohickey' is not defined
get_random_tweet.py
import twitter
api = twitter.Api(consumer_key='',
consumer_secret='',
access_token_secret='')
timeline = api.GetUserTimeline(screen_name='realDonaldTrump',
include_rts=False,
trim_user=True,
exclude_replies=True,
count=6)
def doohickey():
pprint(timeline)
return {'index': "<i> something </i>"}
My views.py
from django.shortcuts import render
from django.http import HttpResponse
from hello.sampled_stream import okdood
import hello.get_random_tweet
from .models import Greeting
# Create your views here.
def index(request):
# return HttpResponse('Hello from Python!')
# okdood()
something = doohickey()
return render(request, "index.html")
I have also attempted the following:
from django.shortcuts import render
from django.http import HttpResponse
from hello.sampled_stream import okdood
from hello.get_random_tweet import doohickey
from .models import Greeting
# Create your views here.
def index(request):
# return HttpResponse('Hello from Python!')
# okdood()
something = doohickey()
return render(request, "index.html")
Error message:
something = doohickey()
NameError: name 'doohickey' is not defined
and
from django.shortcuts import render
from django.http import HttpResponse
from hello.sampled_stream import okdood
import hello.get_random_tweet
from .models import Greeting
# Create your views here.
def index(request):
# return HttpResponse('Hello from Python!')
# okdood()
something = hello.get_random_tweet.doohickey()
return render(request, "index.html")
Error message:
something = hello.get_random_tweet.doohickey()
NameError: name 'doohickey' is not defined

It looks like the issue is that you are not referring to the doohickey function as part of the hello.get_random_tweet namespace. You can do this in several ways:
from django.shortcuts import render
from django.http import HttpResponse
from hello.sampled_stream import okdood
from hello.get_random_tweet import doohickey
from .models import Greeting
# Create your views here.
def index(request):
# return HttpResponse('Hello from Python!')
# okdood()
something = doohickey()
return render(request, "index.html")
or
from django.shortcuts import render
from django.http import HttpResponse
from hello.sampled_stream import okdood
import hello.get_random_tweet
from .models import Greeting
# Create your views here.
def index(request):
# return HttpResponse('Hello from Python!')
# okdood()
something = hello.get_random_tweet.doohickey()
return render(request, "index.html")
As your code is currently structured, you import the hello.get_random_tweet module, but when you refer to doohickey Python is looking for it in the local namespace. However, it should be looking for it in the hello.get_random_tweet namespace. You can either import the function and add it to the local namespace, as shown in the first snippet, or refer to the function in the imported module's namespace as shown in the second snippet.

Might be a copy/paste error, but you're missing endquotes on a few lines here, and the closing bracket:
api = twitter.Api(consumer_key='',
consumer_secret='',
access_token_secret=''
Should be:
api = twitter.Api(consumer_key='',
consumer_secret=''
access_token_key=''
access_token_secret='')

Related

Can't call objects from class in loop Python Django

I an newbee to Django and I realise that it is a very silly question but, when I put objects in a HTML code to call index from data base I recieve just text of what I am calling: List of news(item.title)(item.title)(item.title)(item.title)
views.py:
from django.shortcuts import render
from django.http import HttpResponse
from .models import News
def index(request):
news = News.objects.all()
res = '<hi>List of news</h1>'
for item in news:
res += f'<div><p>(item.title)</p></div>'
return HttpResponse(res)
from django.shortcuts import render
from django.http import HttpResponse
from .models import News
def index(request):
news = News.objects.all()
res = '<hi>List of news</h1>'
for item in news:
res += f'<div><p>{item.title}</p></div>'
return HttpResponse(res)

Exception happened during processing of request from ('127.0.0.1', 9464)

I don't know why this eror I wrote it from a tutorial but It's happend
this is view.py file
here
import view as view
from django.shortcuts import render
from django.http import HttpResponse
from django.views import View
# Create your views here.
from teams.models import Team
class HomePageView(View):
def get(self,requests):
all_teams = Team.objects.all()
context = {"teams": all_teams}
return render(requests,"teamlist.html",context)
this is my admin.py file
this is my admin.py file:here
from django.contrib import admin
# Register your models here.
from teams.models import Team, Player, GameScore
admin.site.register(Team)
admin.site.register(Player)
admin.site.register(GameScore)
that's my first question on stack over flow I don't know I ask True or not . I just need some help :)

How do I import and use the python files in a view in django?

What I want to do is I'd like to call the module( RSAtest ) already created in view in Django. I am calling a function in RSAtest module from AboutPageView. But for some reason, it shows the following error: ModuleNotFoundError: No module named 'webapp.functional' Could you give me an idea how to use modules in Django?
# howdy/views.py
from django.shortcuts import render
from django.views.generic import TemplateView
from webapp.functional import RSAtest
class HomePageView(TemplateView):
def get(self, request, **kwargs):
return render(request, 'index.html', context=None)
# Add this view
class AboutPageView(TemplateView):
RSAtest.main()
#os.system('python3 howdy/RSAtest.py')
template_name = "about.html"
Thanks for the any help.

Django import issues not working propely

i'm passing this guide: http://www.djangobook.com/en/2.0/chapter04.html
It instruct to do:
We no longer have to import get_template, Template, Context, or
HttpResponse. Instead, we import django.shortcuts.render. The import
datetime remains.
However when I'm doing it in my code:
from django.http import Http404
import datetime
from django.shortcuts.render
def current_datetime(request):
now = datetime.datetime.now()
return render(request, 'current_datetime.html', {'current_date': now})
I get these errors:
on import render line:
Expected:import
on render code line:
Undefined variable: render
if I change the import to :
from django.shortcuts import render
then everything is working.
Why is this happaning?
EDIT:
from django.http import Http404
import datetime
import django.shortcuts.render
def current_datetime(request):
now = datetime.datetime.now()
return render(request, 'current_datetime.html', {'current_date': now})
not working either see print screen:
http://i57.tinypic.com/5cyiz7.png
it tells me that the import is unused and still doesn't recognize render
In first case it should be
import django.shortcuts.render as render
and use
return render(request, 'current_datetime.html', {'current_date': now})
If you use only import django.shortcuts.render instead of import django.shortcuts.render as render. Then, return render(request, 'current_datetime.html', {'current_date': now}) won't work. Use return django.shortcuts.render(request, 'current_datetime.html', {'current_date': now}) instead.
from django.shortcuts.render
except this you need to do import django.shortcuts.render.
Or from django.shortcuts import render as you did last.
In your second edit you did import django.shortcuts.render but calling only render so return like this
return django.shortcuts.render(request, 'current_datetime.html', {'current_date': now})
Your final code should be:-
from django.http import Http404
import datetime
import django.shortcuts.render
def current_datetime(request):
now = datetime.datetime.now()
return django.shortcuts.render(request, 'current_datetime.html', {'current_date': now})

Variable import does not work in views.py

Why doesn't the import of a variable outside of a function work in views.py? (ms_fields.py is a file in the same folder)
==== This works: the variable "MS_FIELDS" is imported properly =============
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, RequestContext, get_object_or_404
def current_quote(request):
from .ms_fields import MS_FIELDS #import within the function
return render_to_response('mis/current_quote.html', locals(), context_instance=RequestContext(request))
=== This doesnt work: "local variable 'MS_FIELDS' referenced before assignment"=====
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, RequestContext, get_object_or_404
from .ms_fields import MS_FIELDS # import at the beginning of the file
def current_quote(request):
MS_FIELDS = MS_FIELDS
return render_to_response('mis/current_quote.html', locals(), context_instance=RequestContext(request))
Why is that? Shouldn't the import function make a variable available inside the entire file?
Thanks a lot!
It's not the import that doesn't work, it's the assignment. By assigning to MS_FIELDS within the function, you are telling Python it's a local var, which overrides the global name from the import.
I don't understand why you'd do that anyway. Just pass MS_FIELDS to the context explicitly. Using locals() is a hack, and not a very nice one.

Categories