My website is working well on my local environment, as well as it has been working well on heroku env until my recent deployment.
Code is same on both environments and I referred to all 11 posts related to similar issue,example Reverse for 'todo-user' with arguments '('',)' not found. 1 pattern(s) tried
My issue looks different than what I have seen in other posts here, I think it is related to environment settings/variables, which I am not able to identify it yet.
But the solutions provided on stack overflow, makes me think like the real problem of this issue is something else.
When I try to click on http://127.0.0.1:8000/catalog/mybooks/ link, local website works fine, however, production (heroku), same code throws following exception
NoReverseMatch at /catalog/mybooks/
Reverse for 'book-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['catalog/book/(?P<pk>[0-9]+)$']
Request Method: GET
Request URL: https://<myapp>.herokuapp.com/catalog/mybooks/
Django Version: 2.2.5
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'book-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['catalog/book/(?P<pk>[0-9]+)$']
Exception Location: /app/.heroku/python/lib/python3.7/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 673
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.7.3
Python Path:
['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python37.zip',
'/app/.heroku/python/lib/python3.7',
'/app/.heroku/python/lib/python3.7/lib-dynload',
'/app/.heroku/python/lib/python3.7/site-packages']
Server time: Wed, 9 Oct 2019 04:52:47 +0000
Error during template rendering
In template /app/catalog/templates/base_generic.html, error at line 7
Reverse for 'book-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['catalog/book/(?P<pk>[0-9]+)$']
This looks like a misleading error to me.
Also, because the code is working as expected on my local (ie, showing me all the data), so it doesn't look like a coding issue and so I am not able to understand real problem here (and provided solutions for other similar issues)
I have applied all migrations, so environment looks okay:
$ heroku run python manage.py migrate --remote heroku-prod
Running python manage.py migrate on <my app>... starting, run.5216 (Free)
Running python manage.py migrate on <my app>... connecting, run.5216 (Free)
Running python manage.py migrate on <my app>... up, run.5216 (Free)
Operations to perform:
Apply all migrations: admin, auth, catalog, contenttypes, sessions, social_django
Running migrations:
No migrations to apply.
HP#HP-PC MINGW64 ~/git_projects/prod/django_local_library (master)
Code on DEV/staging, that is also same.
Code:
urls.py for links related to book
urlpatterns = [
path('books/', views.BookListView.as_view(), name='books'),
path('book/<int:pk>', views.BookDetailView.as_view(), name='book-detail'),
path('mybooks/', views.LoanedBooksByUserListView.as_view(), name='my-borrowed'),
]
views.py
class BookDetailView(LoginRequiredMixin, generic.DetailView):
model = Book
def get_context_data(self, **kwargs):
context = super(BookDetailView, self).get_context_data(**kwargs)
process_data(self.request)
return context
class LoanedBooksByUserListView(LoginRequiredMixin, generic.ListView):
"""Generic class-based view listing books on loan to current user. """
model = BookInstance
template_name = 'catalog/bookinstance_list_borrowed_user.html'
def get_queryset(self):
return BookInstance.objects.filter(borrower=self.request.user).filter(status__exact='o').order_by('due_back')
def get_context_data(self, **kwargs):
context = super(LoanedBooksByUserListView, self).get_context_data(**kwargs)
process_data(self.request)
return context
Template
{% for bookinst in bookinstance_list %}
<li class="{% if bookinst.is_overdue %}text-danger{% endif %}">
{{bookinst.book.title}} ({{ bookinst.due_back }})
</li>
{% endfor %}
Yesterday night, after this post, I turned off DEBUG for security purpose, ie set DEBUG=False for staging env, as well as for local and added host on ALLOWED_HOSTS, local and staging work fine, however prod is throwing 500 error:
FYI: staging and prod have same configurations, except host name
Ref Setting DEBUG = False causes 500 Error
If I rollback to previous committed code, it starts throwing error highighted in this ques
And if I verify directly on heroku
HP#HP-PC MINGW64 ~/git_projects/prod/django_local_library (master)
$ heroku run python manage.py runserver --remote heroku-prod
It doesn't throw any exception:
Performing system checks...
System check identified no issues (0 silenced).
October 09, 2019 - 21:18:54
Django version 2.2.5, using settings 'locallibrary.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Even the staging logs have no error.
Please help me if someone has ever faced any such issue.
Solution to my problem was fixed when I followed What is a NoReverseMatch error, and how do I fix it? and NoReverseMatch django - not a valid view function or pattern
And I think all such/similar issues should point to the above link.
Root cause:
Data was corrupted, I deleted corrupted data, and the issue was fixed without any change in the code.
Steps:
I faced similar issue for another new page that I added recently, but the root cause was different (it was due to url formed was incorrect), and the above link helped me identifying the real cause of this issue. I carefully looked into details, including url formed and the page from where url was being referred and finally the data.
And the website currently looks like this https://www.realityseekers.com/
Same problem to me.
All the things were fine except the contents in the table bookinstances.
Somehow, there were several rows in bookinstances misleading, and that rows had not the correspondet row at the table books...
There were bookinstances without books, and when the view tries to get the data, the for bucle got nothing, and when the code: {% url 'book-detail' bookinst.book.pk %}"
tries to do the reverse of nothing, all the things go wrong...
But the dissapointing thing here, is that this kind of error should be best interpreted by Django.
password authentication failed for user "saleor"
FATAL: password authentication failed for user "saleor"
when migrating the first time
if it's something related to database please someone help me to figure this out
I'm using Postgres as the saleor documentation says but I can't pass this migrations point
I read somewhere I have to create a database called saleor with saleor as a user with password 'saleor' who is a superuser and if that is the solution tell me how to do that
DATABASES = {
"default": dj_database_url.config(default="postgres://postgres:12345#localhost:5432/saleor", conn_max_age=600)
}
Postgres is my username, and 12345 is my password for username i.e postgres and saleor is database name. you can change database name to any. If you did not change username,password, and database name the it takes default one.
Step 1: Remove everything inside migration folder for app, except the init.py file.
Step 2: Delete or Truncate Your Current Database which is 'saleor'
Step 3: Create the initial migrations and generate the database schema using folowing commands
python manage.py makemigrations
python manage.py migrate
Step 4: Now you can Create new Super user using following command
python manage.py createsuperuser
When i enter django admin interface and click "Articles" i got Error:
OperationalError at /admin/categories/article/
no such table: categories_article
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/categories/article/
Django Version: 1.9
Exception Type: OperationalError
Exception Value:
no such table: categories_article
Exception Location: \Envs\django19\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 323
Python Executable: \Envs\django19\Scripts\python.exe
Python Version: 3.6.3
admin.py
from django.contrib import admin
from .models import Category, Article
admin.site.register(Category)
admin.site.register(Article)
models.py
from django.db import models
class Category(models.Model):
category_name = models.CharField(max_length=200)
class Article(models.Model):
title = models.CharField(max_length=200)
text = models.TextField(max_length=20000)
What should i do to create new "Article" object in django admin?
The problem is with your migrations but it is difficult to say which file is causing the migration error. You have to check each migration file in migrations directory. It basically occurs because you have a migration in which you have already created the table Articles and somehow django skipped that migration, created the new migration file and marked previous migration as successful. Try one of these ways
1) One way of doing this is find the migration file that creates Article table, delete that migration file and change the dependency of the next migration file to the previous migration file or else django will throw an error. And then:
python manage makemigrations
python manage migrate
2) Change the name of Model Article to something else like Articles and then run migration commands:
python manage makemigrations
python manage migrate
3) If none of the above works. This sure will. Delete all migration and rerun all the migrations.
I'm following this tutorial https://django-oauth-toolkit.readthedocs.io/en/latest/tutorial/tutorial_01.html, and unfortunately I'm stuck in this part
"Point your browser to //localhost:8000/o/applications/ and add an Application instance"
unfortunately when I go to that URL, I get the following error and redirected to
"localhost:8000/accounts/login/?next=/o/applications/"
and page not found error, is there something I'm doing wrong?
The guide doesn't mention it but you need an authenticated session before you can access that page. Add the following steps:
Create a user:
python manage.py createsuperuser
Start up the django server:
python manage.py runserver
Get a validated session by logging into the admin page with the admin account that you just created:
http://localhost:8000/admin
Then navigate to the page the guide references:
http://localhost:8000/o/applications/
I was trying to subclass AbstractUser and stuck in an error on running migrate, and makemigrations says No changes detected
django.db.utils.ProgrammingError: relation "auth_group" does not exist
model:
class SubClient(AbstractUser):
client_id = models.ForeignKey(Client)
phone = models.CharField(max_length=15)
added in settings.py:
AUTH_USER_MODEL = 'myadmin.SubClient'
This error means the auth_group table does not exist in your database. Which means you did not run Django's migration files (python files describing database structure and its changes over time).
As you have your own models, you first need to create migration files for them by running python manage.py makemigrations.
Then run python manage.py migrate to run all migrations (Django's + yours), this will create all database tables (including auth_croup).
Read the doc to lean more about migrations.
when using AbstractUser could i use django's user's builtin password-reset workflow such as password-reset, password-reset-done etc.
the reason i am asking is that i extended user model using AbstractUser but these built-in function not working and i do not get any error but it redirects me to search page and there is no documentation on the internet regarding this issue:
from django.contrib.auth import views as auth_views
path('password-reset/', auth_views.PasswordResetView.as_view(template_name='accounts/password_reset.html'),
name='password-reset'),
path('password-reset/done/',
auth_views.PasswordResetDoneView.as_view(template_name='accounts/password_reset_done.html'),
name='password-reset-done'),
path('password-reset-confirm/<uidb65>/<token>/',
auth_views.PasswordResetConfirmView.as_view(template_name='accounts/password_reset_confirm.html'),
name='password-reset-confirm'),
path('password-reset-complete/s',
auth_views.PasswordResetCompleteView.as_view(template_name='accounts/password_reset_complete.html'),
name='password-reset-complete')