I have trouble installing the django-socialregistration app! - python

I'm a Django amateur, and have problems getting django-registration to work. I followed the installation instructions on their website, but for someone like me these instructions are not 100% clear as to what I should be doing. Here is what I've done:
I installed the oauth2 and python-openid packages using pip. I then copied the facebook.py file from the facebook-python-sdk package to my main django app directory. (As I write this, I'm wondering whether this file should be copied to the socialregistration app directory? Does it make a difference?)
I copied the socialregistration directory to my django project's directory.
I added socialresgitration to my INSTALLED_APPS setting.
To add socialregistration.urls to my urls.py file, I added the following line (not sure if this is correct, since the instructions don't give details):
(r'^social/', include('socialregistration.urls')),
I added the facebook API key and secret key to my settings
I added socialregistration.auth.FacebookAuth to AUTHENTICATION_BACKENDS.
I added socialregistration.middleware.FacebookMiddleware to MIDDLEWARE_CLASSES.
Finally I added the three facebook tags they give in the instructions to one of my templates.
When I then load my website, I get the folllowing error:
Caught AttributeError while rendering: Please add the django.core.context_processors.request context processors to your settings.TEMPLATE_CONTEXT_PROCESSORS set
So, what can I do? I thought installation would be quite simple, but apparently this is not the case. ANY help would be appreciated!
Oh, BTW, I'm using Django 1.2.1 and Python 2.6.
Thanks!

Please add the django.core.context_processors.request context processors to your settings.
Have you done that?
You'll need to change TEMPLATE_CONTEXT_PROCESSORS to include django.core.context_processors.request.

I've found the problem. When my view renders the template, it needs to pass the RequestContext to the template.
return render_to_response('my_template.html', my_data_dictionary, context_instance=RequestContext(request))
Source: http://lincolnloop.com/blog/2008/may/10/getting-requestcontext-your-templates/

Related

Django Template Loaders fail to load templates of my app installed via pip

I need help. I have a Django app which I have uploaded to test.pypi.org so that I can install it in my virtualenv via pip. However, the template loaders fail to look into my app even though it is on my INSTALLED_APPS giving me a TemplateDoesNotExist error message in debug mode. It looks inside django's template directory and on other installed apps, but skips my app. I do not know what else I am missing. I have looked and searched for a solution, however results return problems related to the django apps, which they are working on locally and not installed on virtualenv via pip like me. Thanks in advance.
Only Python modules and packages are included in the package by default. So to add additional files and folder, we’ll need to create a MANIFEST.in file. Like for this case in our MANIFEST.in template folder should include as
recursive-include your_app/templates *

Cannot access rosetta

Versions:
Python 3.5.1
Django 1.10
django-rosetta 0.7.13
The installation guide tells you to add the following to your project's settings.py:
from django.conf import settings
if 'rosetta' in settings.INSTALLED_APPS:
urlpatterns += patterns('',
url(r'^rosetta/', include('rosetta.urls')),
)
However, this just results in an error:
NameError: name 'patterns' is not defined
Searching for that problem reveals that one apparently has to import it:
from django.conf.urls import patterns
But still it doesn't work.
ImportError: cannot import name 'patterns'
This function was removed in django 1.10. However, one can add the rosetta urls conditionally using this approach:
from django.conf import settings
if 'rosetta' in settings.INSTALLED_APPS:
urlpatterns.append(url(r'^rosetta/', include('rosetta.urls')))
However, if you try to access rosetta at the url http://127.0.0.1:8000/rosetta/ you might be surprised to find that you still get a 404 Page not found.
So it seems that the included patterns are not working properly. But they are. The problem is that there's a hidden requirement that one must be logged in when accessing the rosetta page (maybe with a staff/super user?). So, simply go to http://127.0.0.1:8000/admin/, log in, and then go to the rosetta url again. Now it should work.
The installation does note this, sort of:
Because Rosetta requires write access to some of the files in your
Django project, access to the application is restricted to the
administrator user only (as defined in your project’s Admin interface)
How does it know you are an administrator if you are not logged in? It doesn't, and apparently instead of giving an informative error, it ignores the rosetta urls entirely.

need to package jinja2 template for python

(UPDATE: I've made a better question with a better answer here. I was going to delete this question, but some of the answers might prove useful to future searchers.)
My question is just about identical to this, but that answer is ugly (requires a dir structure including sharedtemplates/templates/templates/), incomplete as posted (user "answered" his own question), and assumes some knowledge I don't have.
I'm working on my first python-backed web application. The javascript component is well under development using a static HTML page. Now I want a server-side python component to handle AJAX calls and render an HTML template using jinja2.
I've had success with python before, creating GUI apps using tkinter/requests. Love the language, but the python environment (environments?) is confusing. I'm not working in a virtualenv.
According to jinja2 docs, HTML templates have to be in something called a package. Then you create an Environment with a PackageLoader that knows the name of the package and the template dir:
from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('yourapplication', 'templates'))
So, here's my index.py (it's just a stub and doesn't even try to render anything, but you can at least tell if it crashes).
#!/usr/bin/python
from jinja2 import Environment, PackageLoader # no prob, jinja2 correctly installed using pip
env = Environment(loader=PackageLoader('mypkg', 'template')) # causes server error
# if it doesn't crash, just put up a basic html page for now
print ("Content-type: text/html\r\n\r\n")
print("<html><head><title>hello</title></head><body>hello wuld</body></html>")
Here's the directory structure:
index.py
mypkg/
mypkg/template/index.html
mypkg/__init__.py # empty
Relevant line from error log:
ImportError: No module named mypkg
Maybe I need to structure this differently, and I'm pretty sure I'll need to create and invoke a setup.py to install the module. That's part of what the other answer left out: what's in setup.py and how does it work in this case? I've looked at dozens of resources on setup.py and none of them seems to pertain to the question of installing HTML templates.
Thanks in advance for your help!
UPDATE: fragilewindows pointed to a resource that tells about "developer mode", which is probably part of the answer. The difficulty here is, I'm looking to package this template for local deployment, not for distribution. But 99% of the online documentation is about packaging projects for PyPi. I don't need to package a "project", just a dinky HTML template. Indeed, the only reason I need to package the template is because that's the default way for jinja2 to access templates (and I do want to go native in python).
I just need to convince the environment that "mypkg" is installed, and that "template" is a directory within the install. You can see that my efforts so far are naive; I expect the right answer will be correspondingly lightweight.
I've discovered a WORKAROUND, here. In this usage, the template is not part of a module or package; it is loaded directly from the file system. File system:
./index.py
./template.html
index.py:
#!/usr/bin/python
import jinja2
templateLoader = jinja2.FileSystemLoader( searchpath="." )
templateEnv = jinja2.Environment( loader=templateLoader )
TEMPLATE_FILE = "template.html"
template = templateEnv.get_template( TEMPLATE_FILE )
outputText = template.render( ) # this is where to put args to the template renderer
print ("Content-type: text/html\r\n\r\n")
print(outputText)
I don't know the process involved with packaging but I figure since Jinja2 is written in Python, the process would be the same as packaging any other application in Python.
Here are a few links that may be useful to you:
The Hitchhiker's Guide to Python (great resource): Packaging Explained
Alternatives to Packaging: freeze your application
Python Packaging User Guide (probably the most useful to you)
I hope this helps.
Maybe an issue is in PYTHONPATH . According to Python documantation https://docs.python.org/2.7/tutorial/modules.html#the-module-search-path
it searches for modules in :
the directory containing the input script (or the current directory).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
the installation-dependent default.
You can add some code to your application - printing sys.path variable to make sure your applcation's path is also there.
import sys
from jinja2 import Environment, PackageLoader # no prob, jinja2 correctly installed using pip
print sys.path # printing your sys.path
env = Environment(loader=PackageLoader('mypkg', 'template')) # causes server error
If it's not You can add by
sys.path.append(path_to_your_application)

ImportError: cannot import name memoize

I have a little problem with project upgrade from Django 1.7.1 to 1.9.0.
Every 'RemovedInDjango20Warning' has been fixed, but one thing still left and I don't know how to deal with it.
When I'm trying to visit any page, there is always an ImportError like:
**TemplateSyntaxError at /auth/**
'crispy_forms_tags' is not a valid tag library:
ImportError raised loading crispy_forms.templatetags.crispy_forms_tags:
cannot import name memoize***
I have included {% load crispy_forms_tags %} in my template site, and added 'crispy_forms' in INSTALLED_APPS in settings.py
I tried to find any solution, but without success, before the update worked fine.
What am I doing wrong?
The reason for this error, as has been stated, is that Django dropped memoize at some point, so the version of crispy-forms you were trying to use was not working with the Django version.
Solutions include trying a different version of Django, or trying a different version of crispy-forms. If you are using an unreleased version of Django, then there may not be a version of cripsy that supports it (yet).
However, your error ("cannot import name memoize"), can show up for released versions of Django - I had that error, and a search lead me to this ticket. The issue was that i was using an old version of crispy (1.4.1), and a recent version of Django (1.11.1).
To find a version of crispy to use, you can check the github release page:
https://github.com/django-crispy-forms/django-crispy-forms/releases
The release comments include some information about the Django version supported by different releases.

Django docutils not working

I'm trying to enable docutils for django on windows 7. I've enabled the links in urls.y and settings.py and downloaded and installed the latest snapshot of docutils. However every time I try to access the documentation link in the admin I get a page asking me to install docutils.
Any ideas?
Thanks
I guess django tries to do
import docutils
And this fails. Django catches the exception and displayes this message to you.
Please try to get the real exception. You could insert the above line in one of your views:
def myview(request, ...):
import docutils
I hope django shows you the ImportError. Post it here, if still can't fix it.
Did you restart the Django server? Django has to restart to recognize the newly installed admindocs.
any chance you missed importing the admin in the urls.py?
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
You might have installed the docutils module in the virtual env/path.
Uninstall it from the virtual path and re-install it in the global python installation folder. That should solve the problem.
Note: Do not install django-docutils, but just simply docutils

Categories

Resources