I am following this tutorial for email verification.The version the author is working with is old.I got an error that said
Reverse for ‘activate’ with keyword arguments ‘{‘uidb64’: b’OA’, ‘token’: ‘4tm-3fcfb375c8ba14f9a95b’} . I got that fixed through the first comment . The email got sent .But the link led to www.example.com . The second comment tells how to fix that . The comment was :
For those are using Django 3, You should change some code
Six is deprecated in Django 3, you can use ‘import six’ instead of ‘from django.utils import six’
To send html email, add
email.content_subtype = “html”
after EmailMessage Object.
activate url should be
path(‘activate//’, views.activate, name=’activate’),
get_current_site(request) will return example.com as default when SITE_ID=1 in your settings.py. Add your site name and domail in admin site (/admin/sites/site/) and replace SITE_ID with your sites pk.
But I did not understand how to set SITE_ID to my sites pk.
In settings.py set
BASE_URL = 'https://www.yourdomainname.com'
Don't include trailing /
Also, you have to mention this line in the same file
SITE_ID = 1
Moreover, go to your django admin pannel , default is /admin, and go to the sites tab/model and add/edit your site according to your ip/domainName and display name . This is the important part to do.
If you are testing locally for now then you can use 127.0.0.1:8000 in your ip/domainName and yourdomainNAme.com in display name
If you are deploying or using live then you can use actual ip of your server into ip/domainname or also you can use your domain name (as it's mapped to your ip via DNS system) and display name will be the same.
Hope, it will resolve your problem or issue.
Related
I am currently following a tutorial which introduces a custom Bootstrap 3 template and builds a Django site using it. In the tutorial they suggest that one changes the following template snippet:
<a class="navbar-brand" href="index.html">Start Bootstrap</a>
to the following snippet.
<a class="navbar-brand" href="/">{{ request.site.name }}</a>
However, when I make this change, no site name shows. I am wondering where I should be setting this name. If it helps, I am using Django CMS and there is only one site called example.compopulated in the Sites section of the Administration.
Had the same problem, found this in the documentation.
If you often use this pattern:
from django.contrib.sites.models import Site
def my_view(request):
site = Site.objects.get_current()
... there is simple way to avoid repetitions. Add django.contrib.sites.middleware.CurrentSiteMiddleware to MIDDLEWARE_CLASSES. The middleware sets the site attribute on every request object, so you can use request.site to get the current site.
You can add site name or change existing site called example.com in the Sites section of the Administration.
To access Site object you need to provide SITE_ID in your settings.py
If you are changing example.com then use SITE_ID = 1 in settings
To render site name in django templates, get value from site model using
from django.contrib.sites.models import Site
current_domain = Site.objects.get_current().domain
then pass current_domain to template
It seems that you have to enable the sites framework, refer to doc.
first yo have to understand that in the setting you have a site_id set to 1 and in database migrations an example site with domain name example.com is created. So, you have two options:
You can change this default from django site models like this: python manage.py shell -c "from django.contrib.sites.models import Site; Site.objects.filter(domain='example.com').update(name='My Site', domain='mysite.tld')" then you restart the server.
You can insert a new row in the site models like this: python manage.py shell -c "from django.contrib.sites.models import Site; mysite,_=Site.objects.get_or_create(id=101, name='mysite.tld', domain='mysite.tld'); print(mysite.id, mysite.name)" Then change the site_id in the settings to the new site_id which should be 2 since it is the second new row.
You can take a look at this Github trend for more clearity.
When using django-allauth to do an OAuth login via a social provider, sometimes it fails with the error page "Social Network Login Failure". There is no log output containing more information. There is a feature request for this log output (https://github.com/pennersr/django-allauth/issues/1120) but it has been open for over a year. In the mean time, how do I get more information to debug this error?
More information is passed to the context used to render the error template but is not used in the default template.
You can get log output by overriding the template and including in your template the following:
{{ auth_error }}
or alternatively:
Code: {{ auth_error.code }}, Error: {{ auth_error.exception }}
To override the template, add a folder to your Django template DIRS. In Django 1.8+, this looks like the following:
TEMPLATES = [
{
...
DIRS: [os.path.join(BASE_DIR, 'templates')]
}
]
Then, in that folder, make directory socialaccount and put in it a file called authentication_error.html
If you use a custom SocialAccountAdapter (can be set in your settings.py, read more here) then you can simply overwrite the function authentication_error to log all of the errors.
The function signature (source here) looks like this:
(main/wherever.py):
class SocialAccountAdapter(DefaultSocialAccountAdapter):
def authentication_error(self, request, provider_id, error, exception, extra_context):
your_log_function(
'SocialAccount authentication error!',
'error',
request,
extra_data = {'provider_id': provider_id, 'error': error.__str__(), 'exception': exception.__str__(), 'extra_context': extra_context},
)
(and in settings.py)
SOCIALACCOUNT_ADAPTER = "main.wherever.SocialAccountAdapter"
I'm doing this in my app and it works great!
#Zags answers is great and pointed me in the right direction.
Building on that answer, I suggest start by copying the default authentication_error.html to your tree, e.g.:
$ find /wherever/your/python/is -name socialaccount
$ cp .../that/dir/socialaccount/authentication_error.html \
./myapp/templates/allauth/socialaccount/
Then add in to the copy of the template:
<p>
Code: {{ auth_error.code }}, Error: {{ auth_error.exception }}
</p>
(Technically you could just edit the installed python version...)
Result for me, slightly formatted:
Code: unknown,
Error: Error retrieving access token:
b'{"error":{
"message":"This IP can\'t make requests for that application.",
"type":"OAuthException",
"code":5,
"fbtrace_id":"..."
}}'
"Social Network Login Failure" can occur due to multiple reasons but URL mismatch is the most frequent problem I have experienced:
If you are facing this problem while hosting on third party, do remember to change expected URL to host's url
If the problem is occuring during development only, you must have registered on social website using localhost or 127.0.0.1 . Do call social website through correct url. For e.g. facebook doesn't allow to register 127.0.0.1 but localhost and "manage.py runserver" gives 127.0.0.1 as default. So open your website using "localhost".
Also ensure that you have added correct url to 'sites' in django admin and it has been correctly added to 'social application'
This also happened to me when I was checking https site on http (if I could redirect production). Error code/message was empty.
I commented out these for the http testing:
# SESSION_COOKIE_SECURE = True
# CSRF_COOKIE_SECURE = True
# APPEND_SLASH = True
# PREPEND_WWW = True
When I was testing locally, my problem was two fold:
First, I had to make sure that I started the server using
python manage.py runserver localhost:8000
Then, in the admin page I had to make sure the Sites table had an entry for localhost:8000, e.g. at
http://localhost:8000/en/admin/sites/site/
The gotcha for me was that I'd had multiple attempts at this and the result was the the ID for my localhost:8000 Sites table entry had become greater than 1. You'll notice in the allauth documentation that is says to to have SITE_ID = 1 in the settings.py but if you inadvertently create a different ID for the entry, you'll have to either alter the database entry's ID or change SITE_ID value.
The other issue I had (unrelated to the poster's issue) was that I also had old registrations url template include interfering with the allauth ones.
In my case I had everything ok except the flag SESSION_COOKIE_DOMAIN = mydomain.com which I only had to comment because I was working on localhost and not in mydomain.com.
I have a django app myapp, which registers a menu-command foo to be called via ./manage.py foo.
The command should send a reminder email to all users. The mail contains a link in it, to a specific view, lets say bar. bar has the this entry in the urls.py:
url(r'^(?P<id>\d+)/$', views.bar, name='bar'),
How do I correctly determine the absolute url in this context?
The relative url would be reverse('myapp:bar'). But how do I get the base to that?
I could just hardcode it in settings.py of my project, but that would go against the philosophy of django, wouldn't it?
I'm not sure if it's very specific but I think what I am going to ask is being used in various places in different contexts. My problem is mainly related to URL patterns that I've created for my Django application. I need a profile URL for all application users, so I am creating a URL pattern as below.
urlpatterns = patterns('apps.user_profile.views',
url(r"^(?P<username>\w+)/$", 'user_profile', name="user_profile_page"),
url(r"^app/$", 'app', name="app_page"),
)
As is very clear, I am mapping URLs with user names in the path to fetch user data dynamically. This is working fine, but the problem comes when the system gets a request for app page. In this case, the request goes to user profile, since it accepts all kinds of words and is ordered before app view in urls.py.
Question :
Is there any way to specify, where a request is not resolved in user_profile view, to continue looking at other URLs in the urls.py of app?
Patterns are matched in the order they are written, so simply moving your app pattern before your username pattern will solve your problem:
urlpatterns = patterns('apps.user_profile.views',
url(r"^app/$", 'app', name="app_page"),
url(r"^(?P<username>\w+)/$", 'user_profile', name="user_profile_page"),
)
Now, only if a url doesn't match app/ it will be sent to your user profile view.
Of course, you will have an issue the day a user signs up with the username of app.
I have install Django admin & created some active user & group by admin page.
I need to do login form & views, which will check if user is valid or not do task in the basis of permission.
I have tried following steps.(for reference)
Copied admin login.html for testing & paste it foo_project/templates/registration/login.html
Added in urls.py
from django.contrib.auth.views import login
url(r'^login/', login),
Now by running 127.0.0.1:8080/login
When I am entering valid user-name & password its trying to open /accounts/profile/ & it's not found in urls.py. And if I am entering invalid username or password its doing nothing.
So I simple need to link a page if login successful(user created by admin) & check which type of permission & group he is.Admin created auth_user table in my db.sqlite3
I am new to Django & using version 1.6.
I read document & tried built-in login() in views.py. Got unsuccess.
Is there any built-in for above need. Please describe in depth if possible.
In settings.py create this entry:
LOGIN_REDIRECT_URL = "your_redirect_url"
The user will be redirected to this page after login. Then on the url you will create which will respond to "your_redirect_url" (and should be defined somewhere in your urls.py), you can check the permissions, or groups. For more help about checking permissions, groups, you can find it here.
The login_required decorator can be really useful on implementing your view for your "redirect_url", because you don't want anonymous users accessing to this part of the site, right?