Image is not displaying Django + Angular - angularjs

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

Related

Django URL configuration to accomodate views

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)`

Changing Template Location in Wagtail

I'm trying to direct wagtail to use a template in a centralised location at the top of the project tree. So for example:
Project
|_ HomePage
|_ Search
|_ Mypage
|_ templates
My project re-uses templates and I'd like to give UX/UI Developers access to a single folder rather than multiple sub-folders in multiple pages.
I've tried in Mypage/models.py
class AdvisorSummaryPages(Page):
intro = models.CharField(max_length=250, blank=True, null=True)
content_panels = Page.content_panels + [
FieldPanel('intro'),
InlinePanel('carousell', heading="Carousell Images")
]
template = "advisors.html"
With the following template setting in Project/Project/settings/base.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
With no luck. I can't seem to find any solution on SO or through the documentation or Google that might work. There is a solution presented here using separate model admins but that doesn't work for me. How might I specify the location of the template differently to a subdirectory of templates in the MyPage App?
Thanks
Below is how I organize templates and static assets. I have a themes folder that is located in the main project folder with named theme subfolders within the themes folder. I then have the following in settings/base.py:
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
THEMES_URL = '/themes/'
THEME_PATH = os.path.join(BASE_DIR, THEMES_URL.replace('/', ''), 'name_of_theme')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
THEME_PATH,
...other directories...,
],
... other TEMPLATES-RELATED CODE
}
]
STATICFILES_DIRS = [
os.path.join(THEME_PATH, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
The name_of_theme folder in the THEME_PATH definition contains all of the templates and static files for the theme. In order for the static files to be collected correctly, the folder structure for the css, js, etc. files within each theme folder needs to be:
/themes/name_of_theme/static/name_of_theme/js (or css, etc.)/filename.js (or filename.css, etc.)
The /name_of_theme/static/name_of_theme/ namespacing is necessary for collectstatic to be able to collect the files correctly (see Staticfile namespacing here for more info). When including a reference to a static file in a template, you then do:
{% static 'name_of_theme/js/filename.js' %}
The STATICFILES_DIRS definition is only set up for one theme. You would need to change or add to that if you're using more than one theme.
Some time ago I also came across this Wagtail package: Wagtail Themes. It looks very interesting, but I'm not sure if it provides for handling static files organized within named theme folders as I describe above.

Wagtail 2.2 references API v1

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")

Gae webapp2 routing problems. match the uris wrong

I'm having difficulties with the gae webapp2 routing sistem.
In my routes.py I have the following:
_route = [
RedirectRoute('/', 'home.HomeHandler', name='home', strict_slash=True),
RedirectRoute('/users/<usercode>', 'users.UserSingleHandler', name='user-page', strict_slash=True),
RedirectRoute('/users/comments/new/', 'users.UserNewCommentHandler', name='new-comment', strict_slash=True)
]
The problem I have is that when doing a ajax call to '/users/comments/new' the handler that receives the call is UserSingleHandler and not the one I need (UserNewCommentHandler).
When inspecting the code, I found that de usercode param in UserSingleHandler gets '/comments/new/'... weird!!
¿What I'm doing wrong?
Route like '/users/' will catch all requests on '/users/*', so you can fix your problem by changing the order of routes:
_route = [
RedirectRoute('/', 'home.HomeHandler', name='home', strict_slash=True),
RedirectRoute('/users/comments/new/', 'users.UserNewCommentHandler', name='new-comment', strict_slash=True)
RedirectRoute('/users/<usercode>', 'users.UserSingleHandler', name='user-page', strict_slash=True),
]

What's the best practice to use named Route in AppEngine?

In the app.yaml file, I have put 2 lines to specify the url mapping:
url: /blog/.*
script: blog.app
url: /
script: home.app
the problem is I can't use the "uri_for" function to generate a url for blog module in home.py, case there's no Route added in home moudle:
here is the code in home module:
app = webapp2.WSGIApplication([
webapp2.Route(r'/', handler=HomeHandler, name='home')
], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)
and code in blog.py:
app = webapp2.WSGIApplication([
webapp2.Route(r'/blog/<blog_id:\d+>', handler=BlogHandler, name="blog")
], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)
so, if I have code like this: {{ uri_for('blog', blog_id=blabla) }} in home.html, it can't work.
You should consolidate those routes into one app.
app = webapp2.WSGIApplication([
webapp2.Route(r'/', handler=HomeHandler, name='home'),
webapp2.Route(r'/blog/<blog_id:\d+>', handler=BlogHandler, name="blog")
], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)
and actually those are only the view blog post routes.
If you wanted to do a full CRUD app, you might need to add some more.
app = webapp2.WSGIApplication([
webapp2.Route(r'/admin/blog', handler='admin.AdminBlogHandler:list, name="admin.blog.list"),
webapp2.Route(r'/admin/blog/new', handler='admin.AdminBlogHandler:new', name='admin.blog.edit'),
webapp2.Route(r'/admin/blog/<id:[^/]+>/edit', handler='admin.AdminBlogHandler:edit', name='admin.blog.edit'),
webapp2.Route(r'/admin/blog/<id:[^/]+>', handler='admin.AdminBlogHandler:view', name='admin.blog.view')
], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)
Note for these examples:
1) you prefix a name to load the handlers from a different file (admin.AdminBlogHandler will look in 'admin.py' for 'class AdminBlogHandler'
2) you specify the method to run after the handler name, after the colon.
3) in each method I am creating functionality for get and post, so there are not discrete RESTful URLs for edit and update.

Resources