?: (urls.E006) The STATIC_URL setting must end with a slash - python

I was trying to migrate my new schema for my database in django when I got this error:
AttributeError: 'PosixPath' object has no attribute 'startswith'
I looked online and I saw that if you wrap your paths with str(), the error goes away.
But then I got this error:
?: (urls.E006) The STATIC_URL setting must end with a slash.
I tried doing the obvious thing and changing STATIC_URL = BASE_DIR.parent / "svelte" / "public"(what I think was causing the error) That didn't work, so I tried STATIC_URL = BASE_DIR.parent / "svelte" / "public" /. That also didn't work, so know I need help.

Related

Django-Oscar: Adding slugs to url

I would like to be able to add a the following: [(?P<slug>[-\w]+)] after /dashboard/' between that and the rest of any possible url that comes after, i.e. '/dashboard/ (?P<slug>[-\w]+) /catalogue/. The point of this would be to make it 'partner-specific' for my particular project.
How would I go about doing this? I currently am working directly with Oscar code for several reasons, meaning that I do not have to 'fork' any apps - I just change the Oscar code within my project. The strategy I am trying to accomplish this URL-change is to go into app.py and adding it there after 'dashboard', or going into dashboard/app.py and adding it before each url defined there.
Whenever I change I keep getting 'NoReverseMatch at ---' and errors like:
Reverse for 'MY-URL-IS-HERE' with no arguments not found. 1 pattern(s) tried: ['dashboard/(?P<slug>[-\\w]+)/logout/$']
I am familiar with this error, but now seem unable to ever locate the exact location of the error message. And whenever I do get the page to load (was able to when adding it before 'reviews'), I still get an error something like the following in the terminal:
Invalid URL name dashboard:reviews-list Traceback (most recent call
last): File
"/Users/myname/Desktop/Developer/wybe-all/wybe/apps/oscar/dashboard/nav.py",
line 83, in default_access_fn
If someone that has done this before perhaps could point me in the right direction, that would be greatly appreciated!
Best regards,
William Karlsson

"TypeError: make_staticdir() got an unexpected keyword argument 'document_root'" running Diazo through Proxy

I exactly followed the steps on the Diazo quickstart guide hosted at docs.plone.org (docs.diazo.org is out of date as I write this in April 2015) and received a strange error when I attempted to execute the final command bin/gearbox serve --reload -c proxy.ini. This error prevented me from continuing and did not indicate where it was coming from.
The precise error that I received was:
File "eggs/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/util.py", line 55, in fix_call
val = callable(*args, **kw)
TypeError: make_staticdir() got an unexpected keyword argument 'document_root'
Does anyone know how I can resolve this issue in order to proceed in developing my Plone theme?
It turns out that this error is caused by a mistake in the proxy.ini file due to which a suppressed error is thrown in the webobentrypoints library. To correct this, please change the third section of proxy.ini entitled "app:static" to use "path" instead of the incorrect "document_root" such that it now contains the following:
[app:static]
use = egg:webobentrypoints#staticdir
path = %(here)s/theme
Once you fix this issue, however, you will still need to fix one more problem: the page that the quickstart guide attempts to proxy has changed such that it will redirect your browser and not display your theme. I had good luck switching the final section of proxy.ini to the latest version of the same document now hosted at plone.org:
[app:content]
use = egg:webobentrypoints#proxy
address = http://docs.plone.org/external/diazo/docs/index.html
suppress_http_headers = accept-encoding connection
To properly theme this site you must now further modify the rules.xml file to render the class ".content-column" instead of ".content." It should look like:
<rules
xmlns="http://namespaces.plone.org/diazo"
xmlns:css="http://namespaces.plone.org/diazo/css"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<theme href="theme/theme.html" />
<drop css:content="#indices-and-tables" />
<replace css:theme-children="#content" css:content-children=".content-column" />
</rules>
Hopefully this will fix your problem.
Note: I have already submitted these changes for inclusion in the Diazo documentation but, especially given how many versions of the document can be easily found when looking around for help, it seems prudent to document this possible error in a central location.

In interpreting Jinja2 templates, why does code object have a null filename?

I'm trying to pick apart Jinja2's TemplateSyntaxError to see why it doesn't tell me the exact file name in which a syntax error is found.
I'm actually introducing this error in a sub-template on purpose to try to better understand this templating system. Upon getting the syntax error, I see File "<unknown>", line 4, in template in my Flask preview server. The line number is correct, but the debugger appears confused about the file from which the problem originated, which is very annoying. I'm uncertain as of yet what the name of the code object, template represents.
As someone has pointed out, the <unknown> is used here as a throwaway when the filename value of the code object is null. After reading through a few references for code objects, I've not yet had luck wrapping my head around this weirdness.
Someone appears to get a similar error in this github issue.
May it be that it's just an arbitrary value provided by Jinja2 to some dynamically generated code?
>>> code = compile('print("test")', '<unknown>', 'exec')
>>> code
<code object <module> at 0x1064b6e30, file "<unknown>", line 1>
>>> exec code
test
>>> code.co_filename
'<unknown>'
And there it seems to be indeed in jinja2/debug.py - translate_syntax_error.
The explanation of why that is is rather straightforward. The whole machinery, starting with flask.render_template_string through jinja2.Environment.from_string down to Jinja2 exception handlers does not take any concern about the origin of the template string passed in.
While it would be possible to tunnel some more information top down, what would be the benefit of it anyway? In case of an exception you get a complete stack trace with appropriate local information available on each level of it, including from where you passed in the string and the line number in the template string that erred, e.g.:
File "jinja2-uknown-filename.py", line 7, in index
return flask.render_template_string("this is a \n \n {% test %}")
...
File "<unknown>", line 3, in template
TemplateSyntaxError: Encountered unknown tag 'test'.

Django Tutorial - TemplateDoesNotExist at /polls/

I have tried to get this to work a million times. I have lef it alone for a week and come back. I have Googled and read every post pertaining to this. I have let insecure morons belittle in messages groups without ever finding the answer. I just want this to work. When I am following along in part three of the the Django tutorial, I get to the part where you make the template for the index page, and when I go to check it in the browser, it comes up with TemplateDoesNotExist at /polls/. I have checked file ownership, moved it all around, always changing the TEMPLATES_DIR in setting.py. I have pipe'd the contents of the file to cat to make sure it works. I have scarificed animals to strange Gods to get this to work. I turn to you now. I am sure it is the stupidest thing ever, I have very little doubt about this. I just want it to work.
I am not sure what parts of the code/traceback you want/need here, let me know I will post them. I am doing this on Ubuntu 10.10
EDIT
From settings.py:
TEMPLATE_DIRS = (
"home/kevin/first/tutorial/temps"
)
This used to live in ~, but I moved into the project folder thinking it would help.
Structure(leaving out all complied python files):
~/first/tutorial/:
__init__.py,
manage.py,
polls,
settings.py,
temps,
tut.db,
urls.py
temps:
index.html
polls:
admin.py,
__init__.py,
models.py,
tests.py,
views.py,
>>> TEMPLATE_DIRS = ( "home/kevin/first/tutorial/temps" )
>>> print TEMPLATE_DIRS
home/kevin/first/tutorial/temps
>>> type(TEMPLATE_DIRS)
<type 'str'>
This is a string, not a tuple.
TEMPLATE_DIRS = ( "home/kevin/first/tutorial/temps", )
That is a tuple. A little bit of a Python gotcha.
Furthermore, use an absolute path rather than a relative path.
First off, and this might not be relevant to your problem, but it's good practice to not use absolute paths in your files. I always have the following in my settings.py:
PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates'),
)
You should always have a comma at the end of an element in your tuple, even if it is the only one or the last one, so that Python actually considers it to be a tuple and not evaluate to any other type.
You should also make sure that your TEMPLATE_LOADERS setting contains or looks like this:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
If that doesn't fix it (you should still keep the above in regardless), the problem is most likely related to the template you're rendering to. Confirm that the string of the template you're using in the view corresponds correctly to the relative path of the template within your templates directory. In other words, if the template string is 'polls/index.html' in your view, ensure that the file is actually located at templates/polls/index.html.
What I've used in my settings.py configuration file was:
"TEMPLATE_DIRS = (
'/path/to/folder/Python-templates',
)"
And it worked. What I guess (and correct me if I'm wrong) is that the example in the django tutorial is making reference to the exact location of the template within the templates folder, which is polls. Thus, in the example found in this tutorial, the polls/index.html path to file is saying "the index.html file in the polls folder that is already in the template folder specified in settings.py".
This happened to me because I skipped to part three (I wanted to learn about templates) and I forgot to add add the PollsConfig to INSTALLED_APPS
Your mysite/settings.py should look this this:
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
After I did this change, the templates were all found.
This is a bit confusing because you don't have to do this to make your views work. But templates definitely won't load if your app's config is not in INSTALLED_APPS.
Some things to check:
Do you use an absolute path to specify the template directory in settings.py?
Did you put the template directly into the TEMPLATE_DIR directory, or did you make a polls subfolder to put the template in?
I had the same issue as you described, doing the third part of the Django tutorial throws TemplateDoesNotExist.
Restarting the server solved it for me (did not change any configurations). Hope that helps...

Django i18n: Common causes for translations not appearing

I am making a multilingual Django website. I created a messages file, populated and compiled it. I checked the site (the admin in this case,) in my wanted language (Hebrew) and most phrases appear in Hebrew like they should, but some don't. I checked the source and these still appear as _('Whatever') like they should, also they are translated on the messages file, and yes, I remembered to do compilemessages.
What are some common causes for translations not to appear like that?
Maybe the translated strings are marked as fuzzy?
Just got hit by one. I had the locale/ directory in the root of my project, but by default Django looks for translations in the INSTALLED_APPS directories, and in the default translations. So it didn't find the translations I added. But some of my strings were in the default translations that come with Django (eg "Search") so a few strings were translated, which confused me.
To add the directory where my translations were to the list of places that Django will look for translations, I had to set the LOCALE_PATHS setting. So in my case, where the locale/ directory and settings.py were both in the root of the django project I could put the following in settings.py:
from os import path
LOCALE_PATHS = (
path.join(path.abspath(path.dirname(__file__)), 'locale'),
)
I'm trying to provide a complete check list:
In settings.py, are USE_I18N, USE_L10N, LANGUAGE_CODE, and LOCALE_PATHS set properly?
See this list for all allowed values of language identifiers. Note that Simplified Chinese is specified by zh-hans, not zh-cn.
In settings.py, is django.middleware.locale.LocaleMiddleware included in MIDDLEWARE in correct order?
Have you (re)run django-admin makemessages -l <locale-name> with the correct local name, from the correct place?
You can see an incomplete list of allowed locale name by running ls your/path/to/python/site-packages/django/conf/locale/ on your machine, or by taking a look at the source code
Note that you have to use _ rather than - here. For example, to specify simplified Chinese, execute django-admin makemessages -l zh_Hans, not zh_CN or zh_hans or zh-Hans or anything else.
Have you removed all fuzzy tags in your PO file(s)?
Have you (re)compiled the OP file(s) with django-admin compilemessages?
Have you restarted the web server?
Additional notes:
If some of your translation is overridden by Django's default translation, use contextual markers to bypass it. For example,
models.py
first_name = models.CharField(
pgettext_lazy('override default', 'first name'),
max_length=30
)
last_name = models.CharField(
pgettext_lazy('override default', 'last name'),
max_length=150
)
django.po
#: models.py:51
msgctxt "override default"
msgid "first name"
msgstr "姓"
#: models.py:55
msgctxt "override default"
msgid "last name"
msgstr "名"
and you'll see 姓, 名 instead of the default 姓氏, 名字.
I was having this problem with my project right now. I had the variable LANGUAGES on settings.py set this way:
LANGUAGES = (
('en', _('English')),
('pt-br', _('Brazilian Portuguese')),
)
And a folder structure with a locale folder, and a subfolder pt-br inside. Turns out that my translations weren't loading. The LANGUAGES variable follows the pt-br pattern, and the folders must be on pt_BR pattern. At least that's the only way it's working here!
Hi just attach some fixes I had to do in the past:
Restart the webserver!
In the setting file
- USE_I18N = True is needed
django.middleware.locale.LocaleMiddleware among middleware modules (but this is not your case for sure as long as Django will not care about your local at all)
django.core.context_processors.i18n among TEMPLATE_CONTEXT_PROCESSORS
for sure I had other issues related to translations but I don't remember them all... hope this can help!
A possible cause is Lazy Translation.
In example, in views.py you should use ugettext:
from django.utils.translation import ugettext as _
But in models.py, you should use ugettext_lazy:
from django.utils.translation import ugettext_lazy as _
Another cause can be a wrong directory structure.
Read well the manage command's error message about which directory to create before running the makemassages command for the app translation. (It must be locale for an app, not conf/locale.) Note that the management commands work fine even with the wrong directory structure.
I noticed that when I had % in my text, the translated text was not being used. There may be other characters that can cause this problem. I fixed the problem by escaping the % as %%.
My answer here, all my translations were working except for 2 DateFields that I was using in a ModelForm.
Turns out that I had a widget in my forms.py that was not working well with the translations.
I just removed it for now so I can enjoy Xmas =D

Categories