Coder Social home page Coder Social logo

sibtc / django-beginners-guide Goto Github PK

View Code? Open in Web Editor NEW
1.2K 1.2K 381.0 406 KB

A Complete Beginner's Guide to Django - Code Samples

Home Page: https://www.djangoboards.com

License: MIT License

Python 68.29% HTML 30.66% CSS 0.79% Shell 0.26%
django python tutorial

django-beginners-guide's People

Contributors

mohi7solanki avatar vitorfs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-beginners-guide's Issues

getting an error while using LogoutView.as_view() in main urls.py file

My main urls.py file looks like this...
urlpatterns = [
url(r'^$', views.home, name='home'), # the route for home
url(r'^signup/$', accounts_views.signup, name='signup'),
url('r^logout/$', auth_views.LogoutView.as_view(), name='logout'), # this is a Django class-based view
url(r'^boards/(?P\d+)/$', views.board_topics, name='board_topics'), # listing topics based on
Board
url(r'^boards/(?P\d+)/new/$', views.new_topic, name='new_topic'),
url(r'^admin/', admin.site.urls),

]

The error i am getting is as follows:
url('r^logout/$', auth_views.LogoutView.as_view(), name='logout'),
AttributeError: module 'django.contrib.auth.views' has no attribute 'LogoutView'

How do i fix this?

NOT NULL constraint failed: boards_topic.starter_id

Getting the following error:

Things that I've tried to resolve it:

1- Reset migrations
2- Grep around the different files to find the "boards_topic" pattern, only found it on the db.sqlite binary. Which is created from the models and in the models.py there is no "boards_topic" - So no sure whats going on here.. Please advise.

(django-env) Keiths-MacBook:myproject keithlow$ python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..................................E.......

ERROR: test_csrf (boards.tests.tests_views.NewTopicTests)

Traceback (most recent call last):
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: boards_topic.starter_id

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users/keithlow/Desktop/django-folder/myproject/myproject/boards/tests/tests_views.py", line 94, in test_csrf
response = self.client.post(url,data)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/test/client.py", line 525, in post
response = super().post(path, data=data, content_type=content_type, secure=secure, **extra)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/test/client.py", line 341, in post
secure=secure, **extra)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/test/client.py", line 404, in generic
return self.request(**r)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/test/client.py", line 485, in request
raise exc_value
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/keithlow/Desktop/django-folder/myproject/myproject/boards/views.py", line 32, in new_topic
topic.save()
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/models/base.py", line 729, in save
force_update=force_update, update_fields=update_fields)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/models/base.py", line 759, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/models/base.py", line 842, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/models/base.py", line 880, in _do_insert
using=using, raw=raw)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/models/query.py", line 1125, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1285, in execute_sql
cursor.execute(sql, params)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/utils.py", line 89, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/keithlow/Desktop/django-folder/django-env/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: boards_topic.starter_id

Can you upgrade your guide to support django 2.1.*?

@vitorfs
FYI, I liked the complete guide that was written by you.

  • It was very easy to understand and follow.
  • I was impressed at your coding standards.

If you can upgrade the docs, it will be very useful for us.
I'm thinking that there might be new features which I'm missing in this guide.

Thanks in advance

Still error

Hi, victor. I replaced widget-tweaks with crispy-forms yesterday. It’s ok. But my issue is still there. I dont know what the error ‘boundfield found’ for and how to fix it.

Test Case

class LoginRequiredPasswordChangeTests(TestCase):
def test_redirection(self):
url = reverse('password_change')
login_url = reverse('login')
response = self.client.get(url)
self.assertRedirects(response, f'{login_url}?next={url}')

Noticed this causes test to fail and in PyCharm the miscellaneous 'f' highlights file in red.

Test cases failing with 404 error

Hello!

I am following this guide and stumbled on an weird issue. The website works perfectly when i try it in the web browser. But it seems that all the test cases are failing with 404 error page not found (even though i can access everything myself via web browser).
OS: Debian GNU/Linux 9.4 (stretch), Python: Python 3.5.3, Django: (2, 0, 7, 'final', 0).
urls.py
#######
from django.contrib import admin
from django.urls import path, re_path
from boards import views as board_views
from django.conf.urls import url

urlpatterns = [
path('', board_views.home, name='home'),
path('boards/int:pk/', board_views.board_topics, name='board_topics'),
path('boards/int:pk/new/', board_views.new_topic, name='new_topic'),
path('admin/', admin.site.urls),
]
#######

#######
views.py:
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from .models import Board, Topic, Post

def board_topics(request, pk):
board = get_object_or_404(Board, pk=pk)
return render(request, 'topics.html', {'board': board})

tests.py:
class BoardTopicsTests(TestCase):
def setUp(self):
Board.objects.create(name='Django', description='Django board.')

def test_board_topics_view_success_status_code(self):
     url = reverse('board_topics', kwargs={'pk': 1})
     response = self.client.get(url)
     self.assertEquals(response.status_code, 200)

When i print the variables, i can see that DB object exists, but 404 page not found is returned:
<QuerySet [<Board: Django>]>
b'

Not Found

The requested URL /boards/1/ was not found on this server.

'
404

Thanks!

"TypeError" after running 'python manage.py migrate'

Getting the following errors after running 'python manage.py migrate' during installation

TypeError: init() missing 1 required positional argument: 'on_delete'

-----------------SOLVED BY EDITING THE boards\models.py file---------------------

on line 28, added "on_delete=models.CASCADE"
board = models.ForeignKey(Board, on_delete=models.CASCADE, related_name='topics')
on line 29, added "on_delete=models.CASCADE"
starter = models.ForeignKey(User, on_delete=models.CASCADE, related_name='topics')
on line 57, added "on_delete=models.CASCADE"
topic = models.ForeignKey(Topic, on_delete=models.CASCADE, related_name='posts')
on line 60, added "on_delete=models.CASCADE"
created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
on line 61, added "on_delete=models.CASCADE"
updated_by = models.ForeignKey(User, on_delete=models.CASCADE, null=True, related_name='+')

a question about the project

Hi Vitor,

Thank you for your amazing Django tutorial !
when i use it locally,but once i click the sign up button ,it has a erro,it's a little strange,i dont know how to deal with it,i'm a newer.

In template /Users/franky/Desktop/code/django-beginners-guide/templates/includes/form.html, error at line 11

Exception Type: | TypeError
sequence item 3: expected str instance, BoundField found

Error during template rendering

Hi Vitor ,
I follow your tutorails is good till A Complete Beginner's Guide to Django - Part 4 section "Template Tags".
it shows the error as below:

boundfield found

i am using django2.0.

To Delete a Topic or Replies

I am not able to implement Delete a Topic or Replies. tried various ways but getting errors.
please help me out

Thanks

Getting Error Generator expression in widgets.py

### when i migrate after the " pip install -r requirements.txt" then i got the error

"File "C:\Users\QAMAR\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\admin\widgets.py", line 151

'%s=%s' % (k, v) for k, v in params.items(),
^

SyntaxError: Generator expression must be parenthesized"

thank you

Just want to put in a big thank you for making a clean sample like this it's neat and very useful.

Cheers :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.