Can I deploy a Django App if there is no "" (empty) url? - django

I'm trying to deploy a Django App with railways for the first time and I was wondering where exactly will be my landing page. So basically my urls.py looks something like this:
path('home/', views.home, name="home"),
path('blogposts/', views.blogposts, name="blogposts"),
path('posts/', views.posts, name="posts"),
After deploying my Blog website, let's say with the domain 12345xxx.com, where will I land?

You will get an error if you don't include a folder in the URL, as the URL patten won't be matched and the existance of non-admin URLs stops the default django page from showing. This will be either a 404 error or a 'Django tried these URL patterns, in this order:' type error if you have DEBUG=True on in settings.
Note that you don't have to provide a path (the path can be an empty string), and views can have multiple paths. In this case, perhaps
path('', views.home, name="home"),
path('home/', views.home, name="home_folder"),
path('blogposts/', views.blogposts, name="blogposts"),
path('posts/', views.posts, name="posts"),
would avoid an error.

Related

Hyperlinks in Django Quill Editor are not displaying as expected

Problem
I added the Quill Editor to my Django admin. When I input hyperlinks in the QuillEditor such as "www.example.com", instead of creating the hyperlink exactly as I type it, the URL will appear on the template page as "localhost:8000/plants/www.example.com". Directing me to a broken page instead of www.example.com
Context
I've read through the full quill documentation but I don't see a way to make sure hyperlinks added in the QuillEditor display without the project domain being prepended to the front of the hyperlink url.
Heres how I input the URL 'www.example.com' in the django admin:
Here is how the URL appears on the actual template page (you can see the url in the bottom left when I hover over it:
Maybe I need to edit something in the urls.py?
plants > urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('', include('pages.urls')),
path('plants/', include('plants.urls')),
]
Change your external links to start with "https://".
https://www.example.com

why does <myproject>/accounts/profile/ show the <myproject>/profile/ page

Using django-allauth, after a successful login a user is redirected to http://<myproject>/accounts/profile/... However this URL doesn't exists, but yet it still successfully shows view from http://<myproject>/profile/
settings.py
urlpatterns = [
path('', include('pages.urls')),
path('admin/', admin.site.urls),
url(r'^accounts/', include('allauth.urls')),
url('album/', include('albums.urls')),
url('profile/', include('user_profile.urls')),
]
user_profile\urls.py
urlpatterns = [
path('', views.profile, name='profile'),
]
using show_urls I don't see anything for /accounts/* which would call the view.profile view
/accounts/confirm-email/ allauth.account.views.EmailVerificationSentView account_email_verification_sent
/accounts/confirm-email/<key>/ allauth.account.views.ConfirmEmailView account_confirm_email
/accounts/email/ allauth.account.views.EmailView account_email
/accounts/inactive/ allauth.account.views.AccountInactiveView account_inactive
/accounts/login/ allauth.account.views.LoginView account_login
/accounts/logout/ allauth.account.views.LogoutView account_logout
/accounts/password/change/ allauth.account.views.PasswordChangeView account_change_password
/accounts/password/reset/ allauth.account.views.PasswordResetView account_reset_password
/accounts/password/reset/done/ allauth.account.views.PasswordResetDoneView account_reset_password_done
/accounts/password/reset/key/<uidb36>-<key>/ allauth.account.views.PasswordResetFromKeyView account_reset_password_from_key
/accounts/password/reset/key/done/ allauth.account.views.PasswordResetFromKeyDoneView account_reset_password_from_key_done
/accounts/password/set/ allauth.account.views.PasswordSetView account_set_password
/accounts/signup/ allauth.account.views.SignupView account_signup
/accounts/social/connections/ allauth.socialaccount.views.ConnectionsView socialaccount_connections
/accounts/social/login/cancelled/ allauth.socialaccount.views.LoginCancelledView socialaccount_login_cancelled
/accounts/social/login/error/ allauth.socialaccount.views.LoginErrorView socialaccount_login_error
/accounts/social/signup/ allauth.socialaccount.views.SignupView socialaccount_signup
only the actual /profile/ url...
/profile/ user_profile.views.profile profile
help me to understand why /accounts/profile/ is showing the /profile/ view...
Actually redirecting to /accounts/profile/ is default behavior in django. In django global settings, it is defined as LOGIN_REDIRECT_URL. Django expects you to implement this page/url. Django django-allauth also uses same setting to redirect user after login.
If you want to redirect to any other page like /profile/, override this setting in your project settings.
LOGIN_REDIRECT_URL = '/profile/'
Or if you want django-allauth not to redirect at all set LOGIN_REDIRECT_URL = False in your settings as described here.
Your profile url path is not being limited to match only the start of the url:
url('profile/', include('user_profile.urls')),
This will match anything like gibberishprofile/ for example, including accounts/profile/. If you change the url config to
url('^profile/', include('user_profile.urls')), # note the ^ before profile
Then only profile/ will match.

Django breaks on urls containing "logout"

I'm working on a Django web-app which has user accounts, and so has login and logout functions.
What I would like to have is mywebsite.com/accounts/login/ be the login page. This is working as expected right now.
This issue is with logout- what I would like to have is mywebsite.com/accounts/logout/ logout the user and redirect them to the login page. This, however, doesn't appear to work.
Here is my url configuration in accounts/urls.py:
from . import views
urlpatterns = [
url(r'^login/$', views.LoginView.as_view(), name='login'),
url(r'^logout/$', auth_views.logout, name='logout'),
]
With this configuration, login works fine. But when I go to mywebsite.com/accounts/logout/, I am just immediately sent back to the page I'm currently on.
However, if I change the logout url:
from . import views
urlpatterns = [
url(r'^login/$', views.LoginView.as_view(), name='login'),
url(r'^logMeOutPlease/$', auth_views.logout, name='logout'),
]
Then mywebsite.com/accounts/login works as intended and mywebsite.com/accounts/logMeOutPlease works as intended. Is there a reason the first configuration won't work?
If I however move the logout functionality to the top level (i.e just mywebsite.com/logout/), then it again works fine.
For reference, this is what is in the "top level" urls file:
urlpatterns = [
# other urls that I can't show here
url(r'^redirect/', include('mywebsite.apps.redirect.urls')),
url(r'^accounts/', include('mywebsite.apps.accounts.urls')),
url(r'^$', RedirectView.as_view(url='redirect/')),
url(r'^admin/', admin.site.urls),
]
I am using Django 2.0.6 and Python 3.5.2, and my laptop is on Ubuntu 16.04.
Ok, so from what I understand so far, that behavior is actually correct. Since, views logic was not provided in your question, I will assume that the views are currently not using any #login_required decorators and the logout view does what it says i.e. logout. So without seeing the views logic we assume there were no instructions about where to redirect to on successful logout and no decorator so opens the same page again.
So here's the advice.
In your settings.py file:-
Add:
LOGOUT_REDIRECT_URL = 'url_you_want_to_redirect_to_on_logout'
This will enable redirecting to the specified url whenever a user logs out.
And if it helps, similarly we also do have a
LOGIN_REDIRECT_URL = 'url_you_want_to_redirect_to_on_login'

Django REST framework 2 example not working

I'm working the very first example of the Django REST framework (version 2). I've followed the Installation and Example instructions to the letter but when I open the API in my browser at http://127.0.0.1:8000/ per their instructions, I get the error shown below. What am I doing wrong? This example seems pretty elementary.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in conf.urls, Django tried these URL patterns, in this order:
^admin/
^api-auth/
The current URL, , didn't match any of these.
If you're using Django 2.x for following that tutorial, I'm afraid it's not updated. I just did exactly what the tutorial says, and in the first try it fails.
Then, I changed my urls.py file as follow:
before:
path('admin/', admin.site.urls),
path(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path(r'^', include(router.urls)),
After:
path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path('', include(router.urls)),
and then did python manage.py runserver, and it's working properly.
you don't have a main url defined, must define a index url something like this:
urlpatterns = [
path('index', views.index, name='index'),
# the next path cath the empty path url www.example.com
re_path('/?$', views.index)
]
the url http://127.0.0.1:8000/ don't exist in your urlpatterns, that's what django is telling you.
to work in django rest framework you must follow the next flowjob:
create yours models
create a serializer for yours models
create a ApiView, ViewSet or Django view where you use your serializers to respond to user requests.
register your views in you urlpattern
if you want to enter in the rest-framework login URLs use the pathlocalhost:8000/api-auth

Django URL changes but doesn't render the proper view

I have a url setup with the following view (the url is in the app and the app urls are included in the project):
url(r'^details/(?P<outage_id>\d+)/$', 'outage.views.outage_details'),
def outage_details(request, outage_id=1):
outage_info = Outages.objects.filter(id=outage_id)
return render_to_response('templates/outage/details.html', {'outage_info': outage_info}, context_instance=RequestContext(request))
When I click on the link from http://localhost:8000 the url in the browser changes to http://localhost:8000/outage/details/1 as it should, but the view doesn't render the right template. The page stays the same. I don't get any errors, the url changes in the browser but the details.html template doesn't render. There is an outage in the DB with an ID of 1.
Any thoughts?
The regular expression r'^details/(?P<outage_id>\d+)/$' does not match the URL http://localhost:8000/outage/details/1. However, it should match the expression r'^outage/details/(?P<outage_id>\d+)/$'.
Perhaps, you can post your entire urls.py to find out which view is actually being called, since you don't get any errors. I suspect your home page is being called for all URLs.
Here is my url setup:
project/urls.py
urlpatterns = patterns('',
url(r'^$', 'outage.views.show_outages'),
url(r'^inventory/', include('inventory.urls')),
url(r'^outage/', include('outage.urls')),
url(r'^login', 'django.contrib.auth.views.login', {'template_name': 'templates/auth/login.html'}),
url(r'^logout', 'django.contrib.auth.views.logout', {'next_page': '/'}),
url(r'^admin/', include(admin.site.urls)),
)
outage/urls.py
urlpatterns = patterns('',
url(r'^', 'outage.views.show_outages'),
url(r'^notes/(?P<outage_id>\d+)/$', 'outage.views.outage_notes', name='notes'),
)
I have since changed the details to notes, since I had another page in a different app with a details url and I didn't want it somehow confusing things.