Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parent view seen as a string in template #283

Closed
VivienGiraud opened this issue Sep 20, 2021 · 10 comments
Closed

Parent view seen as a string in template #283

VivienGiraud opened this issue Sep 20, 2021 · 10 comments

Comments

@VivienGiraud
Copy link

Hi,
I'm trying to register user via django-unicorn but I'm having issue with session:

File "REDACTED/unicorn/app/components/email.py", line 57, in register_email  
    login(self.request, user)  
  File "REDACTED/.venv/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 102, in login  
    if SESSION_KEY in request.session:  
AttributeError: 'NoneType' object has no attribute 'session'

Here is the code I'm running:

def register_email(self):  # TODO: Add rate limiting
            pwd = get_random_string(32)
            user = CustomUser.objects.create_user(email=self.email, password=pwd)
            user = authenticate(email=self.email, password=pwd)
            login(self.request, user)
            print("logged")
            return redirect('/register')

Hope this helps.

@adamghill
Copy link
Owner

It looks like self.request is None. Is this a child component maybe? I think there is an issue with getting the request in that instance: #238. Otherwise, if you can fork my example project replicating the issue that would be super helpful. Thanks!

@VivienGiraud
Copy link
Author

Thanks @adamghill,
I forgot the parent=view paremeter, anyway it still does not works I have this error now :

  File "REDACTED/.venv/lib/python3.9/site-packages/django_unicorn/templatetags/unicorn.py", line 112, in render
    component_id = f"{self.parent.component_id}:{self.component_name}"
AttributeError: 'str' object has no attribute 'component_id'

I tried adding a id=0 but it does not change. I know it's crap but it was just for test.

My code is barely:

{% extends "base.html" %}

{% load unicorn %}

{% block content %}
<SOME HTML>
   unicorn 'email' parent=view %}

The email.html template is:

<div class="mt-10 sm:mt-12">
    <div class="sm:flex">
      <div class="min-w-0 flex-1">
        <label for="email" class="sr-only">Email address</label>
        <input id="email" unicorn:model="email" type="email" placeholder="Enter your email" class="block w-full px-4 py-3 rounded-md border-0 text-base text-gray-900 placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-cyan-400 focus:ring-offset-gray-900">
      </div>
    </div>
    <div class="mt-3">
      <button unicorn:click="register_email" class="block w-full py-3 px-4 rounded-md shadow bg-gradient-to-r from-teal-500 to-cyan-600 text-white font-medium hover:from-teal-600 hover:to-cyan-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-cyan-400 focus:ring-offset-gray-900">Start free trial</button>
    </div>
    <p class="mt-3 text-sm text-gray-300 sm:mt-4">Start your free 14-day trial, no credit card necessary. By providing your email, you agree to our <a href="/terms" class="font-medium text-white">terms or service</a>.</p>
</div>

And the Python part:

from django_unicorn.components import UnicornView
from django.shortcuts import redirect
from django.contrib.auth import login, authenticate
from django.utils.crypto import get_random_string

from app.models import CustomUser


class EmailView(UnicornView):
    email = ""
    def register_email(self):
        pwd = get_random_string(32)
        user = CustomUser.objects.create_user(email=self.email, password=pwd)
        user = authenticate(email=self.email, password=pwd)
        login(self.request, user)
        print("logged")
        return redirect('/register')

@VivienGiraud
Copy link
Author

Hi @adamghill ,
I checked the code but I did understood why this error happened.
Do you have any idea?

@adamghill
Copy link
Owner

It looks like parent is a string when Unicorn is expecting it to be an instance of UnicornView. Based on {% unicorn 'email' parent=view %} I would assume that view maybe in your context is a string?

It's challenging to diagnose without the whole context. If you fork my repo and reproduce your issue or provide a sample repo I can help troubleshoot easier.

For an example of a nested component:

@VivienGiraud
Copy link
Author

Thanks for your explanation, as a matter of fact view seems to be a string.
I didn't find the way to launch the example unfortunatly, could you help me?
BTW where does view comes from?

@VivienGiraud
Copy link
Author

Hi @adamghill
Here it is, the very smallest I can do to reproduce it.
I'm definitely sure I miss something, but what?
https://github.com/VivienGiraud/django-unicorn-crash
Hope this helps

@VivienGiraud VivienGiraud changed the title Unable to get session in request Parent view seen as a string in template Oct 4, 2021
@adamghill
Copy link
Owner

Thanks, I'll take a look at this as soon as possible!

@adamghill
Copy link
Owner

Ok, so at least based on your sample code the components aren't nested, so maybe my initial thought was incorrect. I made a PR at VivienGiraud/django-unicorn-crash#1 which fixes the issues in your sample repo and everything loads for me -- hopefully it all makes sense. Closing this for now, but re-open if needed.

@VivienGiraud
Copy link
Author

Holy crap!
The real problem I had since the beggining was the missing unicorn in the settings.py...
I thought that only django_unicorn was needed :/
Thanks a lot @adamghill !

@adamghill
Copy link
Owner

Yep! Sorry I took us down the wrong path in the beginning! Let me know if you run into other issues going forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants