I've just upgraded to Wagtail 2.2 and I'm getting an error 'wagtailadmin_api_v1 is not a registered namespace'. Part of the upgrade moved to api_v2 (which is in my INSTALLED_APPS). I did a search of the Wagtail code and found references to api_v1 in wagtail.admin.api.urls and wagtail/admin/templates/wagtailadmin/admin_base.html (there were several others in tests). I changed the references to V2, but then got the same error claiming that 'wagtail_api_v2 is not a registered namespace.
My question is whether anyone else has seen a similar problem and second, should there be any references to api_v1 in Wagtail 2.2
I was having the same error while integrating Wagtail 2.5 to an existing Django 2.2 project. My mistake was including Wagtail's urls to a namespaced urls.py.
The wagtailadmin_api_v1 is set on wagtail/admin/api/urls.py
According to the docs for url-configuration , you must include those patterns on your main urls.py.
~/projects/project_name/
manage.py
project_name/
my_app/
models.py
urls.py # Those are app-specific urls
views.py
__init__.py
...
settings.py
urls.py # This is the urls.py you should modify
wsgi.py
__init__.py
...
Pattern to be included:
from django.urls import path, re_path, include
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.core import urls as wagtail_urls
urlpatterns = [
...
re_path(r'^cms/', include(wagtailadmin_urls)),
re_path(r'^documents/', include(wagtaildocs_urls)),
re_path(r'^pages/', include(wagtail_urls)),
...
]
Be sure to include those patterns on your main urls.py (it shouldn't set an app_name variable, or all the patterns will be automatically namespaced.)
Stumbled over the same issue today and this worked for me:
# api views.py
from wagtail.api.v2.router import WagtailAPIRouter
from wagtail.api.v2.views import PagesAPIViewSet
wagtail_api_router = WagtailAPIRouter("cast:api:wagtail")
wagtail_api_router.register_endpoint("pages", PagesAPIViewSet)
# local urls.py
app_name = "api"
urlpatterns = [
...,
# wagtail api
path("wagtail/", include((views.wagtail_api_router.get_urlpatterns(), "api"), namespace="wagtail")),
]
Reverse looks now like this:
reverse("cast:api:wagtail:pages:listing")
Related
I received an error 404 on my site with the following error message on my terminal console
`?: (2_0.W001) Your URL pattern '^(?P<question_id>[0-9]+)/$' [name='detail'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().
?: (2_0.W001) Your URL pattern '^(?P<question_id>[0-9]+)/results$' [name='results'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().
?: (2_0.W001) Your URL pattern '^(?P<question_id>[0-9]+)/vote$' [name='vote'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().
`
I tried the following on the site/url.py
`from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("polls/", include('polls.urls')),
]`
and the following on my polls/url.py
`from django.conf.urls import path
from . import views
urlpatterns = [
path('index', views.index, name='index'),
#127.0.0.1/polls
path(r'^(?P<question_id>[0-9]+)/$', views.detail, name="detail"),
#127.0.0.1/polls/1
path(r'^(?P<question_id>[0-9]+)/results$', views.results, name="results"),
#127.0.0.1/polls/1/results
path(r'^(?P<question_id>[0-9]+)/vote$', views.vote, name="vote"),
#127.0.0.1/polls/1/vote
]`
Note: polls is the name to my app
polls/views.py
`from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse('Awsome guys! This is the index page of our polls application')
def detail(request, question_id):
return HttpResponse("This is the detail view of the question:%s" % question_id)
def results(request, question_id):
return HttpResponse("These are results of the question:%s" % question_id)
def vote(request, question_id):
return HttpResponse("Vote on question:%s" % question_id)`
Earlier I was facing problem when I refresh the page. With this Solution I manage to solve the problem. But after applying this to url pattern, image is not loading properly. If I try to open the source of image in new tab , it redirects me to index page.
When url pattern is url(r'^.*$', IndexView.as_view(), name='index'), image is not displayed but page is refreshed properly .
When url pattern is url(r'^$', IndexView.as_view(), name='index'), image is displayed but page is not refreshed properly (Page not found) error
How to solve this.
Update:
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/v1/', include(router.urls)),
url(r'^api/v1/auth/login/$', LoginView.as_view(), name='login'),
url(r'^api/v1/auth/logout/$', LogoutView.as_view(), name='logout'),
url(r'^api/v1/', include(accounts_router.urls)),
url(r'^api/v1/', include(profiles_router.urls)),
url(r'^blogs/',include('blogs.urls')),
url(r'^account_data/',include('customauth.urls')),
url(r'^.*$', IndexView.as_view(), name='index'),
#url(r'^customauth/',include('customauth.urls')),
]
if settings.DEBUG:
urlpatterns+=static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns+=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
In django urls are resolved from list's 0th index, so .* has higher priority than /static/ or /media/, so change the order of urls to get static & media urls higher priority than IndexView.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/v1/', include(router.urls)),
url(r'^api/v1/auth/login/$', LoginView.as_view(), name='login'),
url(r'^api/v1/auth/logout/$', LogoutView.as_view(), name='logout'),
url(r'^api/v1/', include(accounts_router.urls)),
url(r'^api/v1/', include(profiles_router.urls)),
url(r'^blogs/',include('blogs.urls')),
url(r'^account_data/',include('customauth.urls')),
#url(r'^customauth/',include('customauth.urls')), rest of the urls
]
if settings.DEBUG:
# static & media urls
pass
urlpatterns+= [url(r'^.*$', IndexView.as_view(), name='index'),] # accepts any urls otherthan above
I have a function in my views.py that print a string. I have run the local server by django.
I wrote this code.The page of this project must show word "hello world" , but it doesn't!
Will you help me fix it?
from django.conf.urls.defaults import *
from mysite.views import hello
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
('^hello/$', hello),
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
)
Judging by what you have and receiving 404 not found I presume you are just going to localhost:8000
However your url setup says that you should go to localhost:8000/hello/ (you have no page set for the index pattern url(r'^$', hello) )
You also need to follow the correct syntax in your URLs. You currently have:
('^hello/$', hello),
You need to have:
url(r'^hello/$', hello),
You can also enable more verbose debugging by going to your settings.py file and setting DEBUG = True
I have tried everything but it seems that you cannot get a catch all url...
- url: /.*
script: not_found.py
...to work on urls that are based on static directory paths. eg. I can type in www.foobar.com/asdas/asd/asd/asd/ad/sa/das/d and I can get a nice custom 404 page. But if I alter a static path url like www.foobar.com/mydir/mydir/mypage.html, I just get the horrible generic 404....
Error: Not Found
The requested URL /mydir/mydir/mypage.html was not found on this server.
... I would like to alter whatever catches the url in directory paths and writes the 404. This appears the only way to get a consistent custom 404 page in GAE Python.
Can anyone help? I have written my website from scratch and have a very limited knowledge of Python. Achieving a consistent custom 404 is the only thing I cannot seem to overcome.
EDIT/ADD : OK I've added the kind suggestion of #Lipis , and gone through getting started which which thankfully has given me a much better understanding of classes (I sadly can't vote it up yet). But! I am using a .py script found on the net and I think the NotFound class is interfering with the class that gives my index page, because now my index page is the 404 page specified by the Jinja! I have very little understanding of MainHandler so I may have to give up for now.
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import jinja2
class MainHandler(webapp.RequestHandler):
def get (self, q):
if q is None:
q = 'index.html'
path = os.path.join (os.path.dirname (__file__), q)
self.response.headers ['Content-Type'] = 'text/html'
self.response.out.write (template.render (path, {}))
class NotFound(webapp.RequestHandler):
def post(self):
# you need to create the not_found.html file
# check Using Templates from Getting Started for more
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
template = jinja_environment.get_template('404.html')
self.response.out.write(template.render(template_values))
def main ():
application = webapp.WSGIApplication ([('/(.*html)?', MainHandler),('/.*', NotFound)],
debug=True)
util.run_wsgi_app (application)
if __name__ == '__main__':
main ()
For better understanding I'll make some modifications on the Getting Started example which I assume that you have done it and you made some experiments with it.
It's not a good idea to have the static file for all the not found pages in the app.yaml since most likely you would like to show something more dynamic and usually the - url: /.* should be handled within your app.
In this example we are going to add a new RequestHandler for all your not found pages
import jinja2
import os
# more imports
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
class NotFound(webapp.RequestHandler):
def get(self):
# you need to create the not_found.html file
# check Using Templates from Getting Started for more
template = jinja_environment.get_template('not_found.html')
self.response.out.write(template.render(template_values))
application = webapp.WSGIApplication(
[('/', MainPage),
('/.*', NotFound)], # <-- This line is important
debug=True)
But in order to make the jinja2 templates work, follow carefully the modifications that you need to do in Using Templates section from the Getting Started.
The order in the URL mapping is very important so this catch all regular expression (/.*) should be always the last one, because otherwise all the other rules will be skipped.
If you want to catch all URLs, you will have to modify your main request handler in your file "not_found.py" by adding '/.*'.
For example, you can set the file "not_found.py" to:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.out.write("Hello, MAIN!")
application = webapp.WSGIApplication(
[('/.*', MainHandler)], # <--- Add '/.*' here
debug=True)
def main():
run_wsgi_app(application)
If you navigate to www.foobar.com/asd/ad/sa/das/d or any other URL, you will see the message "Hello, MAIN!.
Hope it helps. Ask question if needed
When I query Google AppEngine's datastore using PHP(through Quercus) and the low-level data-access API for an entity, I get an error that the entity doesn't exist, even though I've put it in the datastore previously.
The specific error is "com.caucho.quercus.QuercusException: com.google.appengine.api.datastore.DatastoreService.get: No entity was found matching the key: Test(value1)"
Here's the relevant code -
<?php
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
$testkey = KeyFactory::createKey("Test", "value1");
$ent = new Entity($testkey);
$ent->setProperty("field1", "value2");
$ent->setProperty("field2", "value3");
$dataService = DatastoreServiceFactory::getDatastoreService();
$dataService->put($ent);
echo "Data entered";
try
{
$ent = $dataService->get($testkey);
echo "Data queried - the results are \n";
echo "Field1 has value ".$ent->getProperty("field1")."\n";
echo "Field2 has value ".$ent->getProperty("field2")."\n";
}
catch(EntityNotFoundException $e)
{
echo("<br/>Entity test not found.");
echo("<br/>Stack Trace is:\n");
echo($e);
}
And here's the detailed stack-trace - link.
This same code runs fine in Java (of course after changing the syntax). I wonder what's wrong.
Thanks.
I have found the solution to my problem. It was caused by missing dependencies and I solved it by using the prepackaged PHP Wordpress application available here.
One thing is to be noted. The package overlooked a minor issue in that all files other than the src/ directory need to be in a war/ directory which stays alongside the src/ directory (this as per appengine conventions as mentioned on its documentation). So I organized the files thus myself, put the above PHP file in the war/ directory, and it's working fine on the appengine.